Filter post content in REST API

It looks to me like you’ve written $data->$data[…] where you mean to have $data->data[…]. The “object to string” conversion error is probably due to the second $data, where PHP expects an object’s property name but is getting another reference to the $data object.

Accessing plugin functionality within WP REST API

I have figured out the problem, after trying a load of things I wanted to double check plugins weren’t being loaded at all within the REST API. I added some rudimentary debugging to the plugin in question and I saw that the plugin was indeed being loaded, my issue was that in these conditions the … Read more

Can I define multiple callback methods depending on the call method?

Check if your code looks like this because in the question you pass each method as separate function arguments (I have overlooked it earlier) add_action( ‘rest_api_init’, function () { register_rest_route(‘my-project/v1/’, ‘/form’, array( array(‘methods’ => ‘GET’, ‘callback’ => ‘GET_form’, ), array(‘methods’ => ‘POST’, ‘callback’ => ‘post_form’ ) ) ); }); As you can read in documentation: … Read more

REST-API: extend media-endpoint

I found the solution for my problem. The object type for media is not media, it’s just attachment. So the following code works for me: <?php /** * Plugin Name: REST Response Modifier * Description: A simple plugin to modify the rest api * Author: TheBalco * Author URI: http://somepage.dev */ add_action(‘rest_api_init’, ‘tb_add_custom_rest_fields’); function tb_add_custom_rest_fields() … Read more

Upload image to wordpress using REST API

You can upload images just like your normal PHP/Wordpress file uploads. Reference => wp_handle_upload $mimes = array( ‘bmp’ => ‘image/bmp’, ‘gif’ => ‘image/gif’, ‘jpe’ => ‘image/jpeg’, ‘jpeg’ => ‘image/jpeg’, ‘jpg’ => ‘image/jpeg’, ‘png’ => ‘image/png’, ‘tif’ => ‘image/tiff’, ‘tiff’ => ‘image/tiff’ ); $overrides = array( ‘mimes’ => $mimes, ‘test_form’ => false ); $upload = wp_handle_upload( … Read more

Trying to get an api request getting error 404

Try using a GET request instead of a POST request. The headers indicate they only allow origin from weedmaps.com in the POST request. The method in WordPress is wp_remote_get(). function call_for_api() { $url=”https://api-v2.weedmaps.com/api/v2/listings”; $response = wp_remote_get( $url, array( ‘timeout’ => 45, ‘redirection’ => 5, ‘httpversion’ => ‘1.0’, ‘blocking’ => true, ‘headers’ => array( ‘accept’ => … Read more

Extend WordPress REST API to show slugs for menu items

I had the same challenge, and fixed it by adding it to the response. Example: function get_menu() { # Change ‘menu’ to your own navigation slug. $menu_items = wp_get_nav_menu_items(‘menu’); foreach($menu_items as $menu_item) { // ALTERNATIVE: $slug = get_post_field( ‘post_name’, $menu_item->object_id ); $slug = basename( get_permalink($menu_item->object_id) ); $menu_item->slug = $slug; } return $menu_items; } add_action( ‘rest_api_init’, … Read more

Unable to get the info of the user which doesn’t have created any post via REST API

This is by design and within WP_REST_Users_Controller::get_item_permissions_check() we e.g. have a non-zero check with count_user_posts( $user->ID, $types ). We note that count_user_posts() is filterable with the get_usernumposts filter so one could change 0 to 1, for the /wp/v2/users/\d+ route: add_filter( ‘rest_request_before_callbacks’, function( $response, $handler, $request ){ if ( WP_REST_Server::READABLE !== $request->get_method() ) { return $response; … Read more

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