Wordpress Custom Endpoint shows 404 - wordpress

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

Related

how to make custom wordpress rest api and get posts results with arabic language parameter

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!

Wordpress REST API: Posting data from a form as a guest user

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).

Wordpress custom endpoint returns 404

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.

I want to create a custom URL in wordpress

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.

WordPress REST API not enabled on new account

Based on a quick research, WordPress REST API should be enabled after v4.7 (https://v2.wp-api.org/), however, I cannot access the REST API for my existing or new user by simply appending {name}.wordpress.com/wp-json, ie. https://steventsaotest.wordpress.com/wp-json does not return the expected JSON. Whereas sites like http://www.tribunemedia.com/wp-json works exactly as I wish, as well as my WordPress instance on Digital Ocean.
How can a user whose blog is registered with wordpress.com enable their REST API?
add_action( 'rest_api_init', function () {
$namespace = 'aroma_api';
register_rest_route( $namespace, '/get_signin', array (
'methods' => 'POST',
'callback' => 'get_singnin_data',
));
} );
function get_singnin_data($data)
{
global $wpdb;
$resultdata = array();
$resultdata['message'] = 'Login SuccessFull';
return $resultdata;
}

Resources