Make authorization mandatory on custom routes

Late, but maybe helpful for other readers as I added solution specifically to above code of this question. Solution: Permission Callback function WordPress: version 5.7.2 PHP: version 7.4 host: hostmonster.com client: Windows 10 browsers: tested on Chrome, Firefox, even Edge 😜 worked Code (PHP code in function.php of your installed theme): add_action(‘rest_api_init’, function() { /** … Read more

Authenticating with REST API

You don’t need plugins for authentication unless you’re making a cross domain request, and to get the nonce, you just create it as you would any other nonce. As the handbook states: For developers making manual Ajax requests, the nonce will need to be passed with each request. The API uses nonces with the action … Read more

Updating link on page via REST api

The problem is with your POST request, where the content property should be a plain string and not an object. You should also send just the data that you want to change. When updating a Page (i.e. a post of the type page), you should provide a JSON-encoded or URL-encoded string containing one or more … Read more

current_user_can(‘administrator’) returns false when I’m logged in

Normally for checking for administration privileges you have to check the ā€œmanage_optionsā€ capability, such as: current_user_can(‘manage_options’) Alternatively, you want to list the roles with wp_get_current_user()->roles and ensure the ā€œAdministratorā€ role is in that array. The capabilities you are checking for do not exist in a vanilla install of wp.

Is there anyway to format my EndPoint URL in WordPress?

You don’t. Query args are not part of the route URL. Your endpoint URL is: https://iotkidsiq.com/wp-json/zaindob/v1/sync_order So needs to be registered as: register_rest_route( ‘zaindob/v1’, ‘/sync_order’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘updatetable’, ) ); key and msisdn are arguments that are sent to your endpoint. To define these use set the args property of the … Read more

pagination in WP rest api

define(‘API_ENDPOINT_VERSION’, 1); //flush the rewrite rules on plugin activation function apiendpoint_activate() { flush_rewrite_rules(); } register_activation_hook(__FILE__, ‘apiendpoint_activate’); function apiendpoint_register_endpoints() { register_rest_route( ‘api/v1’, ‘/post’, [ ‘methods’ => ‘GET’, ‘callback’ => ‘api_get_post’, ] ); } add_action(‘rest_api_init’, ‘apiendpoint_register_endpoints’); function api_get_post($request) { $ar = array(‘post_type’ => ‘posts’, ‘posts_per_page’ => 15, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘paged’ => ($_REQUEST[‘paged’] ? … Read more

Get all PDF files from page with WordPress API

You would get the PDFs by querying mime_type=application/pdf So, based on your example your full URL would be: https://www.domain.com/wp-json/wp/v2/media?parent=1267&mime_type=application/pdf However, it is possible when using the parent=ID parameter, you will see ALL the PDFs, not just the attachments specific to that post. This happens when the PDFs were uploaded to the media library, but not … Read more

How do you format the set_body option for WP_Rest_Request?

WP_Rest_Request is not a way to make outgoing calls to remote REST APIs, it’s a data object core uses to pass around information about an incoming request. It’s basically something you recieved, it isn’t something you can send. If you poke your sites REST API with a request, WordPress creates and populates a WP_Rest_Request object … Read more