admin管理员组

文章数量:1320917

I have attempted to register the route, tried all sorts of different parameters, feel my code looks like many of the examples on this site, but can't figure out why it doesn't work.

//Register REST Route
add_action('rest_api_init', 'add_advent_api');

function add_advent_api()
{
    register_rest_route('adventapi/v1', '/adventapi/', array(
        'methods' => 'WP_REST_Server::READABLE',
        'callback' => function () {
            return 'hello';
        },
        'args' => array(
            'adventyear' => array(
                'required' => true,
                'type' => 'integer',
                'description' => 'Year required',
                'minimum' => 1972,
                'maximum' => 9999,
            ),
            'adventday' => array(
                'required' => true,
                'type' => 'integer',
                'minimum' => 1,
                'maximum' => 24,
            ),
        ),
        'permission_callback' => function () {
            return true;
        }
    ));
}

function advent_query(WP_REST_Request $request)
{
    return 'i work';
    /*    $adventyear = $_GET['adventyear'];
    $adventday = $_GET['adventday'];

    $args = array(
        'post_type' => 'advent',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'advent_year',
                'value' => $adventyear,
            ),
            array(
                'key' => 'advent_day',
                'value' => $adventday,
            ),

        ),

    );

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        echo 'i ahve posts';
    } else {
        echo 'No Results';
    } */
}

I have attempted to register the route, tried all sorts of different parameters, feel my code looks like many of the examples on this site, but can't figure out why it doesn't work.

//Register REST Route
add_action('rest_api_init', 'add_advent_api');

function add_advent_api()
{
    register_rest_route('adventapi/v1', '/adventapi/', array(
        'methods' => 'WP_REST_Server::READABLE',
        'callback' => function () {
            return 'hello';
        },
        'args' => array(
            'adventyear' => array(
                'required' => true,
                'type' => 'integer',
                'description' => 'Year required',
                'minimum' => 1972,
                'maximum' => 9999,
            ),
            'adventday' => array(
                'required' => true,
                'type' => 'integer',
                'minimum' => 1,
                'maximum' => 24,
            ),
        ),
        'permission_callback' => function () {
            return true;
        }
    ));
}

function advent_query(WP_REST_Request $request)
{
    return 'i work';
    /*    $adventyear = $_GET['adventyear'];
    $adventday = $_GET['adventday'];

    $args = array(
        'post_type' => 'advent',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'advent_year',
                'value' => $adventyear,
            ),
            array(
                'key' => 'advent_day',
                'value' => $adventday,
            ),

        ),

    );

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        echo 'i ahve posts';
    } else {
        echo 'No Results';
    } */
}
Share Improve this question asked Nov 10, 2020 at 1:07 Kyle DunlopKyle Dunlop 133 bronze badges 2
  • Did you flush your permalinks? – Tom J Nowell Commented Nov 10, 2020 at 1:57
  • Yes, I have tried that. – Kyle Dunlop Commented Nov 10, 2020 at 2:34
Add a comment  | 

1 Answer 1

Reset to default 0

There's an issue in your code: The HTTP method is not valid because WP_REST_Server::READABLE is a class constant, hence it should not be wrapped in quotes:

register_rest_route( 'adventapi/v1', '/adventapi/', array(
//  'methods' => 'WP_REST_Server::READABLE', // wrong
//  'methods' => "WP_REST_Server::READABLE", // wrong
    'methods' => WP_REST_Server::READABLE,   // correct

    // ... other args.
) );

You should also ensure that you use the correct request/HTTP method as well as correct endpoint URL, e.g. https://example/wp-json/adventapi/v1/adventapi, when making requests to the API.

本文标签: