Skip to main content
added 797 characters in body
Source Link
Kevin
  • 181
  • 2
  • 9
// 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_fieldregister_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, 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/"
      }
    ]
  }
}
// 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_field( '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 );
                } ),
            ),
        ) );
    }

} );

And, I cannot see the endpoint in the site's /wp-json/

// 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, 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/"
      }
    ]
  }
}
Source Link
Kevin
  • 181
  • 2
  • 9

Wordpress API "code":"rest_no_route" with Custom Route

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_field( '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 );
                } ),
            ),
        ) );
    }

} );

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/

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)