0

I am attempting to start creating a custom endpoint for one of my sites, so I can pull in ACF fields to another. I have the following code in my theme's functions.php

// register the endpoints needed
add_action( 'rest_api_init', function( ) {

    /*
    // post types to include
    $_pts = array( 'posts', );


    // loop the post types and create a rest endpoint for the ACF fields for them
    foreach ( $_pts as $_pt ) {

        // var_dump($_pt . '/acf');

        // register the rest endpoint
        register_rest_route( 'kp-api/v1', $_pt . '/acf/(?P<id>\d+)', array(
            'methods' => array( 'GET', ),
            'get_callback' => function( $data ) {
                return 'HERE';
            },
            'args' => array(
                'id' => array( 'validate_callback' => function( $param, $request, $key ) {
                    return is_numeric( $param );
                } ),
            ),
        ) );
    }
    */

    // register the rest endpoint
    register_rest_route( 'kp-api/v1', '/TEST/(?P<id>\d+)', array(
        'methods' => array( 'GET', ),
        'get_callback' => function( $data ) {
            return 'HERE';
        },
        'args' => array(
            'id' => array( 'validate_callback' => function( $param, $request, $key ) {
                return is_numeric( $param );
            } ),
        ),
    ) );

} );

And when I try to browse my endpoint, I end up with {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

And, I cannot see the endpoint in the site's /wp-json/. I can however, see that at least the namespace itself is indeed created by browsing to it: /wp-json/kp-api/v1

However, there are no endpoints in it.

{
  "namespace": "kp-api/v1",
  "routes": {
    "/kp-api/v1": {
      "namespace": "kp-api/v1",
      "methods": [
    "GET"
      ],
      "endpoints": [
    {
      "methods": [
        "GET"
      ],
      "args": {
        "namespace": {
          "required": false,
          "default": "kp-api/v1"
        },
        "context": {
          "required": false,
          "default": "view"
        }
      }
    }
      ],
      "_links": {
    "self": "https://example.com/wp-json/kp-api/v1"
      }
    }
  },
  "_links": {
    "up": [
      {
    "href": "https://example.com/wp-json/"
      }
    ]
  }
}

What am I doing wrong? No, I'm not using the wp-json plugin, just the normal built-in Wordpress functionality (v.5.3.2)

1 Answer 1

0

You’re using the wrong function. It’s register_rest_route() not register_rest_field().

3
  • About to edit my code. I realized it after I posted. Does not change... please give me a few minutes to update the question, because I also have more information for it.
    – Kevin
    Commented Mar 9, 2020 at 13:09
  • Ok. Now editted.
    – Kevin
    Commented Mar 9, 2020 at 13:15
  • I think I've got it working now. had to swap get_callback for callback
    – Kevin
    Commented Mar 9, 2020 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.