Site Identity REST Endpoint for Apps and Headless Sites
Adds a public /wp-json/site/v1/identity endpoint with the site name, tagline, logo, site icon, language and home URL, so headless front ends and apps can brand themselves from WordPress.
Opens WPIDE on
Code
add_action( 'rest_api_init', function () {
register_rest_route( 'site/v1', '/identity', array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => function () {
$logo_id = (int) get_theme_mod( 'custom_logo' );
$response = new WP_REST_Response( array(
'name' => get_bloginfo( 'name' ),
'description' => get_bloginfo( 'description' ),
'url' => home_url( '/' ),
'language' => get_bloginfo( 'language' ),
'logo' => $logo_id ? wp_get_attachment_image_url( $logo_id, 'full' ) : null,
'icon' => get_site_icon_url( 512 ) ? get_site_icon_url( 512 ) : null,
'timezone' => wp_timezone_string(),
) );
$response->header( 'Cache-Control', 'public, max-age=3600' );
return $response;
},
) );
} );
Notes
Only public information is returned; the response can be cached by browsers for an hour.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.