close
Skip to content

REST API: Add activate themes endpoint#70847

Open
xavier-lc wants to merge 8 commits intoWordPress:trunkfrom
xavier-lc:add/REST-API-Activate-theme
Open

REST API: Add activate themes endpoint#70847
xavier-lc wants to merge 8 commits intoWordPress:trunkfrom
xavier-lc:add/REST-API-Activate-theme

Conversation

@xavier-lc
Copy link
Copy Markdown
Contributor

@xavier-lc xavier-lc commented Jul 22, 2025

https://core.trac.wordpress.org/ticket/63739

What?

This PR adds the /themes/activate endpoint.

Why?

In order to be able to activate themes via REST API.

How?

The API validation follows the same logic currently used on wp-admin:

https://github.com/WordPress/wordpress-develop/blob/8a3c793e08e4374b3176424a3fece9d0906f4c6c/src/wp-admin/themes.php#L20-L35

Testing Instructions

  1. Checkout this branch
  2. Run npm run dev
  3. Open http://localhost:8888/wp-admin/themes.php
  4. Check the Active theme
  5. Go to http://localhost:8888/wp-admin/post-new.php?gutenberg-demo
  6. On the console, run:
await wp.apiFetch( { url: `/index.php?rest_route=%2Fwp%2Fv2%2Fthemes%2Factivate`, method: 'POST', data: { stylesheet: 'twentyfifteen' } } ); 
  1. Check that the response is { "message": "Theme activated successfully.", "theme": "twentyfifteen" }
  2. Go back to http://localhost:8888/wp-admin/themes.php and check that the active theme is indeed twentyfifteen

Testing Instructions for Keyboard

Screenshots or screencast

Before After

@xavier-lc xavier-lc requested a review from spacedmonkey as a code owner July 22, 2025 16:07
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 22, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @xavier.lozano.carreras@a8c.com, @xavier-lc.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: xavier.lozano.carreras@a8c.com, xavier-lc.

Co-authored-by: spacedmonkey <spacedmonkey@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @xavier-lc! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jul 22, 2025
Xavier Lozano Carreras added 2 commits July 22, 2025 18:12
@spacedmonkey
Copy link
Copy Markdown
Member

There is already a PR for this - #41836

@xavier-lc
Copy link
Copy Markdown
Contributor Author

There is already a PR for this - #41836

@spacedmonkey thanks for pointing it out, I wasn't aware of it. The activate action is done there with the PUT method, right?

Is there any chance of that PR getting merged, given that it's from 3 years ago? Thanks!

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. Core REST API Task Task for Core REST API efforts labels Jul 23, 2025
public function activate_theme( WP_REST_Request $request ) {
$theme = wp_get_theme( $request['stylesheet'] );

switch_theme( $theme->get_stylesheet() );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the theme requirements are not met switch_theme can call wp_die.
Maybe its good to handle this scenario. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked and wp_die takes care of that. It gets into the wp_is_json_request() clause

https://github.com/WordPress/wordpress-develop/blob/54a06f8972bba2443d624372270f1ad666bcc55f/src/wp-includes/functions.php#L3779

And then the API returns this: {code: "wp_die", message: $message, data: {status: 500}, additional_errors: []} where $message is the wp_die param.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core REST API Task Task for Core REST API efforts First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants