Wordpress REST API for favicon and logo images

Viewed 1234

Can someone help me how to get to the endpoint for logo and favicon images in Wordpress rest api? When I use http:// .... / wp-json / endpoint, I'm getting the title or name and description with no links to images?

1 Answers

For logo you need add this function to functions.php:

// wordpress api for logo extension
add_action( 'rest_api_init', 'add_logo_to_JSON' );
function add_logo_to_JSON() {
register_rest_field( 'post', 'page_logo_src', array( // post for where to register - page_logo_src is the name for api
        'get_callback'    => 'get_logo_src',
        'update_callback' => null,
        'schema'          => null,
         )
    );
}

function get_logo_src( $object, $field_name, $request ) {
    $size = 'full';
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $feat_img_array = wp_get_attachment_image_src($custom_logo_id, $size, true);
    return $feat_img_array[0];
}

Then logo is available here: http://your-domain/wp-json/wp/v2/posts under page_logo_src.

You can use this function to grab any of wordpress data you need, for favicon use get_site_icon_url.

Related