WordPress REST API not enabled on new account - wordpress

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;
}

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 Custom Endpoint shows 404

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

Respond to custom URL in wordpress plugin

So, I'm trying to write a wordpress plugin which acts like an API and responds to a specific HTTP request. I cache some data in my plugin and I would like to fetch the data in the other side using AJAX or sth.
What actions or filters should I use? I've tried using request filter but couldn't figure out how it works.
Use the WP REST API for this: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
Below is a simple example of how to add a custom route and how to handle the request.
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/my-custom-route/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
function my_awesome_func( WP_REST_Request $request ) {
// You can access parameters via direct array access on the object:
$param = $request['some_param'];
// Or via the helper method:
$param = $request->get_param( 'some_param' );
// You can get the combined, merged set of parameters:
$parameters = $request->get_params();
// The individual sets of parameters are also available, if needed:
$parameters = $request->get_url_params();
$parameters = $request->get_query_params();
$parameters = $request->get_body_params();
$parameters = $request->get_json_params();
$parameters = $request->get_default_params();
// Uploads aren't merged in, but can be accessed separately:
$parameters = $request->get_file_params();
}

WooCoommerce custom endpoint - Enable auth protection

I have defined the following custom endpoint for woocommerce:
add_action( 'rest_api_init', 'custom_endpoint' );
function custom_endpoint() {
register_rest_route( 'wc/v3', 'my_custom_endpoint', array(
'methods' => 'GET',
'callback' => 'return_value',
) );
}
function return_value() {
return "this is my custom endpoint!";
}
However, this endpoint is also accessible if I'm not authenticated using the ck and cs.
How can I protect it the same way all other, default endpoints of the WooCommerce API are protected? (I would prefer not needing another auth plugin for this to work, but to access it with the standard WooCommerce auth keys instead).
Thanks!
Hello use permission_callback with JWT Authentication for WP REST API plugin so it will work fine.
Steps :
1) Install JWT Authentication for WP REST API plugin
2) Set permission_callback
Below code will work well after JWT Authentication for WP REST API plugin installation
add_action('rest_api_init', 'custom_endpoint');
function custom_endpoint(){
register_rest_route('wc/v3', 'my_custom_endpoint', array(
'methods' => 'GET',
'callback' => 'return_value',
'permission_callback' => function($request){
return is_user_logged_in();
}
));
}
function return_value(){
return "this is my custom endpoint!";
}
for more information please check JWT Authentication for WP REST API documentation.
Checked and works well.
Cookie authentication is the standard authentication method included with WordPress. When you log in to your dashboard, this sets up the cookies correctly for you, so plugin and theme developers need only to have a logged-in user.
As an example, this is how the built-in Javascript client creates the nonce:
<?php
wp_localize_script( 'wp-api', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
This is then used in the base model:
options.beforeSend = function(xhr) {
xhr.setRequestHeader('X-WP-Nonce', wpApiSettings.nonce);
if (beforeSend) {
return beforeSend.apply(this, arguments);
}
};
Here is an example of editing the title of a post, using jQuery AJAX:
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
Note that you do not need to verify that the nonce is valid inside your custom end point. This is automatically done for you in
rest_cookie_check_errors()
Woocommerce API
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#authentication-over-https
While cookie authentication is the only authentication mechanism
available natively within WordPress, plugins may be added to support
alternative modes of authentication that will work from remote
applications.
As Per Official Document : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins

Search all WP REST API Posts with angular

I using angular to display posts with the WP Rest Api. However the search will only filter through the first 99 posts since that is the max. Is there anyway to yield all results?
Create a custom endpoint that utilizes a custom query. Something like this:
add_action( 'rest_api_init', function () {
register_rest_route( 'search', '/all', array(
'methods' => 'GET',
'callback' => 'get_all_posts',
));
});
Endpoint: yoursite.com/wp-json/search/all
function get_all_posts() {
$query = new WP_Query(
array( 'posts_per_page' => -1 )
);
return json_encode( $query );
}
Remember to clear the permalinks when registering new endpoints!
Getting posts is capped at 100 per page, but each request returns the header X-WP-TotalPages which gives you the number of total pages in your request.
So do a request for the next pages...
var url = your-route.example/wp-json/wp/v2/posts?per_page=100
$http.get(url).
success(function(response) {
var pagesNum = response.headers('X-WP-TotalPages')
for (var i = 2; i <= pagesNum; i++) {
$http.get(url+'&page='+i)
.success(function(response){
// do something with the next page data
})
}
});
...and so on, until you've gone through all the available pages.
Source: REST API Handbook

Resources