2

I wanted to get a list of all WordPress themes installed, together with their meta info (e.g. name, status, author etc) using WordPress REST api. I also wanted to be able to activate any theme via my API client.

I've gone through this documentation but I didn't find any relevant end-point.

Is it possible at this moment?

2

You can write your own endpoint and use wp_get_themes to get a list of themes via that. Here is a simple one:

add_action( 'rest_api_init', function () {
    //Path to rest endpoint
    register_rest_route( 'theme_API/v1', '/get_theme_list/', array(
            'methods' => 'GET', 
            'callback' => 'theme_list_function' 
    ) );
});
// Our function to get the themes
function theme_list_function(){
    // Get a list of themes
    $list = wp_get_themes();
    // Return the value
    return $list;
}

Now you can get a list of your themes by accessing http://example.com/wp-json/theme_API/v1/get_theme_list.

I wouldn't suggest activating/deactivating themes via API. It can totally mess up things, such as activated widgets.

| improve this answer | |
  • Thanks, I'll check this code. Further, why activating theme via API will mess with other things, can you please elaborate? I'm trying to build a basic admin panel for WordPress via API. – Faisal Khurshid Jun 23 '17 at 23:28
  • For example, if your new theme doesn't support all the sidebars of your old theme, you will lose some of your widgets (deactivated actually). And some themes depends on specific plugins which demands activation of them too. And so many other stuff, that should be taken care of manually. – Jack Johansson Jun 24 '17 at 0:59
  • I don't think the issue of different themes having different sidebars, thus deactivation of some widgets on theme change is specific to REST api, is it? – Faisal Khurshid Jun 24 '17 at 1:14
  • I've tried the mentioned solution, it returns theme names but not the relevant meta info. The returned result is something like this: {"backup":{"update":false},"theme1":{"update":false},"theme2":{"update":false} ... } – Faisal Khurshid Jun 24 '17 at 3:28
  • 2
    wp_get_themes returns an array of WP_Theme class objects. You can create a forearch and save each of the metadata in an array, and then return the array. Here is the documentation for the returned array. – Jack Johansson Jun 24 '17 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.