Search code examples
wordpresswordpress-themingthemeswordpress-rest-api

Custom End points API to get all themes from word-press returning False value


Here is the custom end points What i have created here for getting all themes. But in json it’s not returning result as expected.

add_action( ‘rest_api_init’, function () {
//Path to rest endpoint
register_rest_route( ‘theme/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;
}

?>

If I can see simply the wp_get_themes() function it will return all the themes and its description in arrays. and It’s return fine in arrays but when i am encoding this into json to pass data its returning only array keys.

Producing only key name like in this way

All: {"basepress":{"update":false},"codilight-lite":{"update":false},"twentyfifteen":{"update":false},"twentyseventeen":{"update":false},"twentysixteen-child":{"update":false},"twentysixteen":{"update":false}}

I need all the information about the themes.

How can i do that with custom REST end points.

Please help.


Solution

  • Try this code

    add_action( 'rest_api_init', function () {
    //Path to rest endpoint
        register_rest_route( 'theme/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();
    
        $varTheme = array();
    
        foreach($list as $theme=>$value){
            $varTheme[$theme] = (array)$value;
        }
        return $varTheme;
    }