Attach featured image to custom endpoints

To attach anything onto the results, you need to loop through each item and set the property on the object before you return it. You can do a for loop or an array_map() which essentially does a loop and sets the value for each item in the return array to what is returned by the … Read more

Extend Woocommerce rest api routes fails

What if in your register_rest_route() call you can pass it the permission_callback option: ‘permission_callback’ => function () { return current_user_can(‘customer’); // OR return current_user_can(‘shop_manager’); } That way you are sure that you are dealing with a logged in Woocommerce user?? These are the two roles that Woocommerce is adding to your site. https://docs.woocommerce.com/document/roles-capabilities/ I know … Read more

Create a new user using WP REST API and declare meta object

Just found the solution in another question of Stack Overflow. Using the function register_meta(): register_meta(‘user’, ‘icq’, array( “type” => “string”, “show_in_rest” => true )); Now I can make a request using: { “username” : “johndoe”, “email”: “[email protected]”, “password”: “qwerty”, “meta”: { “icq”: “11223344” } } And the response are: { “id”: 49, “username”: “johndoe”, “name”: … Read more

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_no_route” – Debug error

You should not GET on a POST only rest callback. The code below creates a POST call only register_rest_route(‘miningRigs/v1’, ‘createRig’, array( ‘methods’ => WP_REST_SERVER::CREATABLE, ‘callback’ => ‘createMiningRig’, )); Check http://localhost/wordpress-site/wp-json/miningRigs/v1 which shows you can only POST on createRig I just copy/paste you code + added the https://pressupinc.com/blog/2013/07/minimum-viable-wordpress-plugin/ then enabled the plugin on 4.9.5 and it … Read more

Rest API Hook When Post Is Requested

My original answer was all wrong, so it’s been removed in it’s entirety. Neither the posts_selection nor wp hook are fired during the REST API request. The hook you need is rest_pre_echo_response. This hook passes three parameters: The response data to send to the client The server instance. The request used to generate the response. … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)