I followed this tutorial to create custom endpoints. But, it always returns 404.
I added code below to function.php
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access:
http://smap.cas.mcmaster.ca/wp-json/awesome/v1/awesomeparams
It shows 404:
I checked many other posts, but still can't find a solution. Can I get some help?
Thanks!
Did you flush your permalinks?
Go to Wordpress admin -> Settings -> Permalinks and click save, It should flush your permalinks and then It should work.
Related
I would like to know if there is a way to prevent cache in wordpress inside the REST API.I have this minimal example:
add_action('rest_api_init', 'get_wp_page_fn2' );
function get_wp_page_fn2(){
register_rest_route( '/wp/v2/tf_api/' ,'get_wp_page2',
array(
'methods' =>'GET',
'callback' => 'get_wp_page_callback2',
)
);
}
function get_wp_page_callback2( $request) {
$response = [
"response" => 1,
"url" => 33,
];
echo json_encode($response);
die();
}
If I change "response" => test, it takes about 5/10 minutes to update it. It's kind of a nightmare to do dev like that.
I try a lot of stuff but nothing seems to be working
Took me like a day.
Since I'm using Mamp pro, I have to deal with this https://documentation-4.mamp.info/en/MAMP-PRO-Mac/Languages/PHP/index.html
I create API route for search
I try to get a parameter search in Arabic containing about (two words)
but it didn't work, I try almost of video, and not working
try Unicode and encode JSON also
Does anybody have a solution for it?
my API route
add_action('rest_api_init', function() {
register_rest_route( 'wm/v1', '/search/(?p<request>.+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'wm_search_post',
) );
enter code here
and the Endpoint callback function
function wm_search_post(WP_REST_Request $request){
$req=JSON_encode($request['request']);
return $req;
// here I try to test the results but get rest_no_route
//404 >>> but it is work if I remove parameter
}}
Your regex expression is not correct and also some of your closing braces missing. Let's see the below code, it's working on my end.
add_action('rest_api_init', function() {
register_rest_route( 'wm/v1', '/search/(?P<request>\w+)/', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'wm_search_post',
));
});
function wm_search_post(WP_REST_Request $request) {
$req= $request['request'];
return rest_ensure_response($req);
}
You can get a response http://localhost/elementskit/wp-json/wm/v1/search/<pass_string_here>
Hope that will work!
I started creating an ajax post form that takes name and email as inputs. The idea is to have the anonymous, un-logged user to fill out the fields, and when the forms posting succeeds on the API, the user gets access to download an eBook.
The admin wants to be able to view the data of all the users that have filled out the form on the backend, so what I did is create a new custom post type labeled "ebook-user".
From the researching that I've done online, it seems that a user could only post on post comments to the rest api. Is there a way to enable anonymous posting on a custom post type?
Right now, when I post with my current code, I get a 401 error:
{
"code": "rest_cannot_create",
"message": "Sorry, you are not allowed to create posts as this user.",
"data": {
"status": 401
}
}
Here's my js code:
fetch(`${site_url}/wp-json/wp/v2/ebook-user`, {
credentials: 'same-origin',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': ajax_nonce,
},
body: JSON.stringify(credentials)
})
.then((res) => res.json())
.then((res) => {
console.log(res)
if(res.data.status == 403) {
formMsg.textContent = `Error`
}
console.log('success');
})
.catch((err) => {
console.error(err);
})
Note that I did add this line in my functions.php, which enables comment posting as guest user
add_filter( 'rest_allow_anonymous_comments', '__return_true' );
Since you want to POST with an un-authenticated user, I'd create a custom endpoint to do it.
Something like:
/wp-json/mytld/v1/ebook
Allow POST to the end point but nothing else.
Validate the input VERY CAREFULLY and I'd go so far as to do things like make sure you don't already know the email address before allowing the post to succeed.
This requires a little more code but gives you a lot more control.
=C=
add_action( 'rest_api_init', function () {
register_rest_route( 'getdata/v1', '/author/(?id)', array(
'methods' => 'GET',
'callback' => 'my_bookdata_func',
) );
} );
function my_bookdata_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
Url to access
wp-json/getdata/v1/author/(?id).
I am stuck on creating custom endpoints. I followed this tutorial to create custom endpoints. But, it always returns 404.
https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
I added code below to function.php:
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access: http://www.my-domain.com/wp-json/awesome/v1/awesomeparams
It shows 404:
I also tried to flush the permalinks by going to WordPress admin -> Settings -> Permalinks and click save.
Can I get some help?
Thanks a lot!
The solution is to use the url below:
http://www.my-domain.com/index.php/wp-json/awesome/v1/awesomeparams
I am trying to create a wordpress plugin to create a custom URL. So that when that URL run in browser my PHP code should run.
I am having the below code in my plugin
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'method' => 'GET',
'callback' => 'custom_phrase',
)
);
}
function custom_phrase() {
return rest_ensure_response( 'Hello World! This is my first REST API' );
}
My plugin got activated in admin side.
But when enter the custom path in browser, its not working
http://localhost/wp/wp-json/my-route/my-phrase
it shows 404 not found.
Please help to find where i have error.
After activating the plugin. Go to WordPress Settings -> Permalinks. Click on save permalinks buttons once. Now the new route will start working.