最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - How to get custom logo with REST API instead of get_theme_mod();

programmeradmin2浏览0评论

Normally I use this snippet to set the a custom logo inside my custom themes. I'm a noob with the theme settings API and settings API so I don't have at the moment a theme options page. I'm developing a vue.js based theme and I want to get all the data using axios. Is there a REST endpoin I can use to get the logo or I need to register a custom route like I'm doing for the menù and other themes resources?

<a class="navbar-brand ml-auto" href="<?php bloginfo('url'); ?>">
      <?php $logo = wp_get_attachment_image_src( get_theme_mod('custom_logo'), 'full' ); ?>
      <?php if( $logo ): ?>
        <img src="<?php //echo $logo[0]; ?>" width="auto" height="75">
      <?php endif; ?>
      </a>

Normally I use this snippet to set the a custom logo inside my custom themes. I'm a noob with the theme settings API and settings API so I don't have at the moment a theme options page. I'm developing a vue.js based theme and I want to get all the data using axios. Is there a REST endpoin I can use to get the logo or I need to register a custom route like I'm doing for the menù and other themes resources?

<a class="navbar-brand ml-auto" href="<?php bloginfo('url'); ?>">
      <?php $logo = wp_get_attachment_image_src( get_theme_mod('custom_logo'), 'full' ); ?>
      <?php if( $logo ): ?>
        <img src="<?php //echo $logo[0]; ?>" width="auto" height="75">
      <?php endif; ?>
      </a>
Share Improve this question asked Apr 6, 2020 at 13:38 sialfasialfa 32910 silver badges29 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Is there a REST endpoint I can use to get the logo

As far as I know, none that's specific to theme mods.

or I need to register a custom route like I'm doing for the menu and other themes resources?

I would not say yes or no, but it's easy to create custom endpoints and you'd also get the data (in the API response) in the format you prefer (string, object, array, etc.), so why not and you could have something simple like:

function my_theme_register_rest_routes() {
    // For retrieving all theme mods.
    // Sample request URL: http://example/wp-json/mytheme/v1/settings?_wpnonce=XXXXXXXXXX
    register_rest_route( 'mytheme/v1', '/settings', [
        'methods'  => 'GET',
        'callback' => function () {
            return get_theme_mods();
        },
        'permission_callback' => function () {
            return current_user_can( 'manage_options' );
        },
    ] );

    // For retrieving a specific theme mod.
    // Sample request URL: http://example/wp-json/mytheme/v1/settings/custom_logo?_wpnonce=XXXXXXXXXX
    register_rest_route( 'mytheme/v1', '/settings/(?P<name>[a-zA-Z0-9\-_]+)', [
        'methods'  => 'GET',
        'callback' => function ( $request ) {
            return get_theme_mod( $request->get_param( 'name' ) );
        },
        'permission_callback' => function () {
            return current_user_can( 'manage_options' );
        },
    ] );
}
add_action( 'rest_api_init', 'my_theme_register_rest_routes' );

But anyway, I thought this question might help you. :)

发布评论

评论列表(0)

  1. 暂无评论