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;
}
Related
I'm trying to add a custom updatable field to show in the User rest api.
I managed to show the field properly on GET requests but it's not updating with new values on POST requests.
This is the code i have on functions.php:
function slug_add_post_data() {
register_rest_field('user',
'ubicaciones',
array(
'get_callback' => 'slug_get_field',
'update_callback' => 'slug_update_field',
'schema' => array(
'description' => 'Ubicaciones usuario',
'type' => 'string',
'context' => array('view', 'edit')
)
)
);
}
add_action('rest_api_init', 'slug_add_post_data');
function slug_get_field($user, $field_name, $request) {
return get_user_meta($user->id, $field_name);
}
function slug_update_field($value, $user, $field_name) {
if (!$value || !is_string($value)) {
return;
}
return update_user_meta($user->ID, $field_name, strip_tags($value));
}
Something must be wrong with the update_callback function, but i can't find exactly what.
Any clues what I'm doing wrong?
Thank you.
I managed to make it work.
this post helped me:
https://wordpress.stackexchange.com/questions/293025/curl-post-work-with-user-meta-but-not-the-custom-user-meta
add_action("rest_api_init", function () {
register_rest_field(
"user"
, "ubicaciones"
,
[
"get_callback" => function ($user, $field_name, $request, $object_type) {
return get_user_meta($user["id"], $field_name, TRUE);
},
"update_callback" => function ($value, $user, $field_name, $request, $object_type) {
update_user_meta($user->ID, $field_name, $value);
},
]
);
});
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',
) );
} );
I am trying to schedule an event on hourly basis, that should store some data to a mlab database(mongo db), but the function is not getting called.
class plug_Activator{
public function __construct() {
add_action( 'my_hourly_event', 'tmpp' );
}
function activate() {
if ( ! wp_next_scheduled( 'tmpp' ) ) {
wp_schedule_event( time() - 3540, 'hourly', 'my_hourly_event');
}
}
function tmpp(){
$args= array(
"website"=> "www.xyz.com",
"post"=> "wasupp"
);
$response = wp_remote_post( 'https://api.mlab.com/api/1/databases/todo/collections/todos?apiKey=xxxx', array(
'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
'body' => json_encode($args),
'method' => 'POST'
));
}}
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!
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.