admin管理员组

文章数量:1391987

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": ""
      }
    }
  },
  "_links": {
    "up": [
      {
    "href": "/"
      }
    ]
  }
}

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)

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/wp-json/kp-api/v1"
      }
    }
  },
  "_links": {
    "up": [
      {
    "href": "https://example/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)

Share Improve this question edited Mar 9, 2020 at 13:11 Kevin asked Mar 9, 2020 at 13:01 KevinKevin 1812 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

本文标签: rest apiWordpress API quotcodequotquotrestnoroutequot with Custom Route