• Resolved Luis Braschi

    (@casper-braske)


    Hi.

    Is there a way to retrieve the value of the rest_base from a specific post type so I can use in my endpoints?
    Something like this:

    $rest_base = get_rest_base('my_type');
    
    wp_localize_script('my-script', 'foo',
         array('bar' => esc_url_raw(rest_url()).'/wp/v2/'.$rest_base)
    );
    

    Best regards.

    • This topic was modified 7 years, 6 months ago by Luis Braschi.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    We don’t have anything specific for that ourselves, but we also tend to not create such functions because logically it’d be part of WordPress Core.

    From my quick testing, you could use get_post_type_object() which fetches all the aspects of a given post type. It’ll return a WP_Post_Type object, at least with WP 4.6, and one of the properties will be rest_base.

    $my_type = get_post_type_object( 'my_type' );
    
    wp_localize_script('my-script', 'foo',
         array('bar' => esc_url_raw(rest_url()).'/wp/v2/'.$my_type->rest_base)
    );
    
    Thread Starter Luis Braschi

    (@casper-braske)

    Got it.
    Didn’t know it was a core feature as it didn’t show up while digging into the reference.

    Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get rest_base value from a post type’ is closed to new replies.