• Resolved whipkey

    (@whipkey)


    I’ve developed a custom plugin for my project implementing several endpoints that take either no or one argument(s) and they are all working great.

    Now I want to get a second parameter but if anything is wrong with the URL at all it will return a 404 so it becomes difficult to figure out what the exact problem is. Here is my latest attempt, which still returns 404:

    add_action( 'rest_api_init', function () {
        register_rest_route( PLUGIN_NAMESPACE, '/v1/getstuff/(?P<slug>[a-z0-9\-]+)/(?P<secondparam>[a-z0-9\-]+)', array(
            'methods' => 'GET',
            'callback' => 'myFunctionToGetStuff',
        ));
    });

    How do I add additional parameters to a custom endpoint?

    https://ww.wp.xz.cn/plugins/rest-api/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    Here is my latest attempt, which still returns 404:

    I’m not sure I follow. What request are you making?

    Thread Starter whipkey

    (@whipkey)

    Sorry. Let’s say my namespace is “myplugin” and my website is “example.com”. This would be an example request:

    http://example.com/wp-json/myplugin/v1/getstuff/first-slug/second-slug

    In my callback function I expect to be able to access (again referencing the example code from the first post) both arguments like this:

    function myFunctionToGetStuff($data) {
        $firstSlug = $data['slug']; // in the above request would be "first-slug"
        $secondSlug = $data['secondparam']; //in the above request would be "second-slug"
    }
    Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    Ok. Given the following code in a mu-plugin:

    add_action( 'rest_api_init', function () {
        register_rest_route( 'myplugin', '/v1/getstuff/(?P<slug>[a-z0-9\-]+)/(?P<secondparam>[a-z0-9\-]+)', array(
            'methods' => 'GET',
            'callback' => 'myFunctionToGetStuff',
        ));
    });
    
    function myFunctionToGetStuff( $request ) {
    	return array( 'hello world' );
    }
    

    When I do GET http://wordpress-develop.dev/wp-json/myplugin/v1/getstuff/first-slug/second-slug, I see:

    [
    "hello world"
    ]
    

    If you’re getting a 404 with that request, then there’s something else going wrong in your codebase.

    How do I add additional parameters to a custom endpoint?

    See: http://v2.wp-api.org/extending/adding/#arguments

    Thread Starter whipkey

    (@whipkey)

    Not sure what I was missing, but I have it sorted out now and working with multiple parameters. Thanks!

    Do you have the final example you were using?

    Hi Bachhuber,

    I just using rest api v2, got route problem about document not clear at http://v2.wp-api.org/extending/adding/#arguments.
    Can you improve docs about ‘/author/(?P<id>\d+)’ pattern ?

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Endpoint with Multiple Arguments/Parameters’ is closed to new replies.