Search code examples
wordpresswordpress-rest-api

Wp Rest Api Custom End point


Im trying to add a custom end point to my wp-rest api the latest version. I have this already but the one with the slug param at the end does not work.. Does any one know why.. would be great if anyone could help..

     register_rest_route( 'wp/v2', '/guestmix', array(
        array(
            'methods'         => WP_REST_Server::READABLE,
            'callback'        => array( $this, 'get_guestmixes' )
        ),
        'schema' => array( $this, 'get_public_item_schema' )
    ) );

    register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\d+)', array(
        'methods' => 'GET',
        'callback' => 'get_guestmix'
    ) );

Solution

  • i guess it because you used d metacharacter for regex (?P<slug>\d+) that's mean for digit, please try use S instead. The code should look like this

    register_rest_route( 'wp/v2', '/guestmix/(?P<slug>\S+)', array(
        'methods' => 'GET',
        'callback' => 'get_guestmix'
    ) );
    

    this is cheat sheet for reference http://www.phpliveregex.com/