Filter post_content before loading in Gutenberg editor

I found that there is no way to change the data which is coming to editor when it is loaded. But it’s possible to replace the data after that. JS: script.js wp.domReady(function () { const newData = window.myNewBlocksData; if (newData) { wp.data.dispatch(‘core/block-editor’).resetBlocks(wp.blocks.parse(newData)); console.log(‘replaced’); } }) PHP: <?php class MyBlocksManipulation { public $prefix = ‘my’; public … Read more

REST API multiple media upload

While not optimal, there is nothing stopping you from sending two requests at the same time and to continue your logic after both responses are received. This is relatively easy with jQuery or JS promises, but most likely possible in all languages which has an API to send HTTP requests asynchronously. Just keep in mind … Read more

Can I authenticate with both WooCommerce consumer key and JWT?

Yes this is possible by structuring your requests appropriately. For system requests use OAuth 1.0 (consumer key as before), but encode it to include the OAuth credentials in the URL not in the headers. Having the OAuth credentials in the Authorisation header triggers the JWT error. GET https://DOMAIN/wp-json/wc/v1/subscriptions * Authorization: `OAuth 1.0` * Consumer key: … Read more

How to use the WP REST API for new user registration (sign up form)?

hopefully you’ve found the answer already. Here’s our solution, for your reference. 😀 The following code should add User Registration via REST API to your WordPress Website. It supports Registration of ‘subscriber’ and ‘customer’. Add it to your function.php add_action(‘rest_api_init’, ‘wp_rest_user_endpoints’); /** * Register a new user * * @param WP_REST_Request $request Full details about … Read more

Match REST API post output from custom endpoint

The prepare_item_for_response method of the WP_REST_Posts_Controller class “Prepares a single post output for response” by translating every property of the post object for output from the REST API. The method doesn’t run statically, so we need to instantiate a controller in our endpoint. Here’s a simple example endpoint: function endpoint( $request ) { $p = … Read more

Is it possible to nest the JSON result of WordPress REST API?

Use below code snippet function video_get_post_meta($object, $field_name, $request) {    return array( ‘video’ => array( ‘length’ => get_post_meta($object[‘id’], ‘length’, true), ‘file_name’ => get_post_meta($object[‘id’], ‘file_name’, true), ‘thumbnail_’ => get_post_meta($object[‘id’], ‘thumbnail_’, true), ‘video_url’ => get_post_meta($object[‘id’], ‘video_url’, true), )); } add_action(‘rest_api_init’, function() {    register_rest_field(‘post’, ‘video’,        array(            ‘get_callback’ => ‘video_get_post_meta’,            ‘update_callback’ => ‘video_update_post_meta’,            ‘schema’ => null        )    ); } Output … Read more

Hiding API routes list

You can use the filter hook ‘rest_index’ : add_filter(‘rest_index’, function ($response) { $data = $response->get_data(); $data[‘namespaces’] = []; $data[‘routes’] = []; return $data; }, 9999, 1); It is possible to remove your route from $data[‘namespaces’] and $data[‘routes’]

wordpress wp-json prefix issue

You need to set the permalink structure of your website. Goto Settings > Permalink. Remove index.php from the Custom Structure. Click on Save Changes. It re-generate your permalink structure. And you’ll be able to access the posts with http://example.com/index.php/wp-json/wp/v2/posts Updated: .htaccess code like below: # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /dev.fresh/ RewriteRule ^index\.php$ … Read more

How to get all posts from parent and children categories?

I had the same use case than you — calling WP-API from React — and ran into the same issue. Unfortunately WP-API does not support filtering by parent categories. In order to support it, you could extend the API’s functionality, adding a new filter that will run a tax_query behind the scenes, similar to Faye’s … Read more