0

Below is the code I am using

add_action( 'rest_api_init', 'add_custom_users_api');

function add_custom_users_api(){
   register_rest_route( 'testing', '/users', array(
     'methods' => 'GET',
     'callback' => 'get_custom_users_data',
   ));
}

function get_custom_users_data($data){
    //get users by market
    $result = "Testing this";
    return $result;
}

Below is the response I am getting

enter image description here

6
  • check this developer.wordpress.org/reference/functions/register_rest_route Commented Nov 13, 2017 at 6:27
  • Thanks for the link. very useful. Can you also please tell me if I do not create any namespace then? Commented Nov 13, 2017 at 7:17
  • namespace for version controller. Are you using version in API? Commented Nov 13, 2017 at 7:21
  • no, I am creating a test API. I have updated my question. Please help Commented Nov 13, 2017 at 7:26
  • you can use simply like register_rest_route( 'testapi', '/apiendpoint', array( 'methods' => 'POST', 'callback' => 'callback_function' )); Commented Nov 13, 2017 at 7:28

1 Answer 1

1

Try This

add_action( 'rest_api_init', 'add_custom_users_api');

function add_custom_users_api(){
    register_rest_route( 'testing', '/users', array(
        'methods' => 'GET',
        'callback' => 'get_custom_users_data',
    ));
}

function get_custom_users_data($data){
    //get users by market
    $result = "Testing this";
    return $result;
}

http://localhost/wordpress/wp-json/testing/users

enter image description here

Sign up to request clarification or add additional context in comments.

11 Comments

It says {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
updated my answer. your project folder is wordpress. right?
No. I didnt change anything.
I have added an image in my question
why you are adding index.php in API endpoint. remove that and try
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.