Wordpress custom API development - wordpress

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

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

Related

How to call a class method as a callback function in wordpress custom endpoint?

I have a custom endpoint which looks like this:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => 'get_user_lang'
));
});
I was able to call the callback function "get_user_lang" when it wasn't a class based method. But once I converted it to a class based method, I wasn't able to call it.
My class looks like this:
<?php
namespace T2mchat\TokenHandler;
class TokenHandler {
function get_user_lang() {
return "client_langs";
}
}
?>
and my new endpoint looks like this:
$t2m = new T2mchat\TokenHandler\TokenHandler();
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($t2m, 'get_user_lang')
));
});
Anyone have any idea on how to call a class based method in WordPress Rest API custom endpoints?
If the hook is called within the class if-self and yout callback method is defined there:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($this,'get_user_lang')
));
});
If from different class:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array(new className,'get_user_lang')
));
});
If this solution is not working, a bit more details of your problem will help in defining.
If you're using a static method of the same class, the array() solution doesn't work. I had to use the "magic" constant __CLASS__ :
'callback' => __CLASS__ . '::get_posts',
And to add an action using this custom class with another static method as callback I had to use this notation:
require_once( get_template_directory() . "/inc/classes/Rest/Posts.php" );
add_action( 'rest_api_init', 'Custom\Namespace\Posts::register_routes');
Class methods in WordPress Hooks should be set via 2 dimensional array.
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($class_object,'get_user_lang')
));
});

WordPress REST API Custom Endpoint with URL Parameter

I am trying to create a custom endpoint for the WordPress REST API and pass parameters through the URL.
The endpoint currently is:
/wp-json/v1/products/81838240219
What I am trying to achieve is an endpoint that looks like this and being able to retrieve the identifier parameter in the callback.
/wp-json/v1/products?identifier=81838240219
// Custom api endpoint test
function my_awesome_func( $data ) {
$identifier = get_query_var( 'identifier' );
return $identifier;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/products=(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
First you need to pass in the namespace to register_rest_route
Like this
add_action( 'rest_api_init', function () {
register_rest_route( 'namespace/v1', '/product/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
Your name space namespace/v1 and your route is /product/{id} like this
/namespace/v1/product/81838240219
and now you can use the route inside your function like this
function my_awesome_func( $data ) {
$product_ID = $data['id'];
}
If you need to add options for ex.
/namespace/v1/product/81838240219?name=Rob
and use it inside the function like this
function my_awesome_func( $data ) {
$product_ID = $data['id'];
$name = $data->get_param( 'name' );
}
The process is very simple but requires you to read this documentation
I modified the provided answer a little to get my desired endpoint:
/wp-json/api/v1/product?identifier=81838240219
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/product/', array(
'methods' => 'GET',
'callback' => 'ea_get_product_data',
) );
} );
function ea_get_product_data( $data ) {
$identifier = $data->get_param( 'identifier' );
return $identifier;
}
If you want to pass alphanumeric parameters, use [a-zA-Z0-9-] instead of \d
add_action( 'rest_api_init', function () {
register_rest_route( 'namespace/v1', '/product/(?P<id>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );

Wordpress REST API create custom GET endpoint from custom table

I'm trying to get some data from a custom database table. But I get a rest forbidden error.
add_action( 'rest_api_init', function () {
register_rest_route( 'emevents/v1', '/all', array(
'methods' => 'GET',
'callback' => 'handle_get_all',
'permission_callback' => function () {
return current_user_can( 'edit_others_posts' );
}
) );
} );
function handle_get_all( $data ) {
global $wpdb;
$query = "SELECT event_name FROM 'wp_em_events'";
$list = $wpdb->get_results($query);
return $list;
}

why wordpress custom rest api route only work for 'GET'

I create a most simple rest api route
public static function register_my_route(){
register_rest_route( 'vender/v1','test',array(
'method' => 'POST',
'callback' => function(){return 1;}
));
}
add_action( 'rest_api_init', array( __CLASS__, 'register_my_route' ) );
I use postman to test it, always return "code": "rest_no_route", 404, but if I change it to method => GET, it works
Try to use the WP_REST_Server constants and also define the permissions for your declaration.
add_action( 'rest_api_init', function() {
register_rest_route( 'vender/v1', '/test', array(
'methods' => WP_REST_Server::CREATEABLE,
'callback' => function() { return 'works'; },
'permission_callback' => function() { if ( ! current_user_can( 'edit_posts' ) ) { return false; } return true; },
) );
} );
Hope this helps!

How to hit the url in WordPress rest_api_init

I have set up the API end points in WordPress in theme functions.php.
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
));
});
And the callback function is like:
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return new WP_Error( 'awesome_no_author', 'Invalid author', array( 'status' => 404 ) );
}
return $posts[0]->post_title;
}
I want to know what will be the url for this API and how can I hit that url?
Please help me out. I know this is simple thing but couldn't figure it out.

Resources