How to retrieve wp_ json_encode data from custom WordPress database table

$wpdb->get_results() returns an Array of rows representing the results. The rows themselves can be represented as objects or arrays depending on the second argument. See the documentation: output_type One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information. OBJECT – result will be output as a numerically … Read more

Unset data in custom post type WordPress API (wp-json)

If possible, only the examples shown in internet is: function qod_remove_extra_data($data, $post, $context) { // We only want to modify the ‘view’ context, for reading posts if ($context !== ‘view’ || is_wp_error($data)) { return $data; } // Here, we unset any data we do not want to see on the front end: unset($data[‘author’]); unset($data[‘status’]); // … Read more

Include custom post meta value in fetched JSON

Two things to look at: 1) register_meta() 2) custom-fields support on post type. 1) register_meta You can register your custom post meta with show_in_rest => true to make it accessible via the api. Bottom of this page: Modifying Responses, provides the following example: <?php // The object type. For custom post types, this is ‘post’; … Read more

Import JSON feed to WordPress

json_decode the JSON into an array. $slices = json_decode(file_get_contents(‘yourJSONFile.json’),true); Loop into the data if ($slices) { foreach ($slices as $slice) { $title = $slice[1]; // insert more logic here } } Create a post programmatically by using wp_insert_post. // Create post object $my_post = array( ‘post_title’ => $title, ‘post_content’ => ‘This is my content’, ‘post_status’ … Read more

What is /wp-json?

That’s the root URL for the REST API. All WordPress installs have it, but in 4.6 very few endpoints exist, mostly oembed and plugins. The core infrastructure for the REST API has been available since 4.5, with functions such as register_rest_route being available. /wp-json itself is generating discovery data, listing the various available endpoints. You … Read more

WP API returning SQL results as strings, rather than numbers

The string output type is expected for $wpdb query results, the db data types are not mapped to the corresponding PHP data types. You will have to take care of it yourself, like: $data = [ ‘int’ => (int) ‘123’, ‘bool’ => (bool) ‘1’, ‘string’ => (string) ‘abc’ ]; return rest_ensure_response( $data ); with the … Read more

Get Image URL instead of Attachment Id in Rest API

You can modifiy REST API responses in themes functions.php like this. function ws_register_images_field() { register_rest_field( ‘post’, ‘images’, array( ‘get_callback’ => ‘ws_get_images_urls’, ‘update_callback’ => null, ‘schema’ => null, ) ); } add_action( ‘rest_api_init’, ‘ws_register_images_field’ ); function ws_get_images_urls( $object, $field_name, $request ) { $medium = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), ‘medium’ ); $medium_url = $medium[‘0’]; $large = wp_get_attachment_image_src( … Read more

What are the Oembed Links For?

Those are links for the wordpress “self” oEmbed. It provides the URLs needed to enable embeding the content of the wordpress site in other sites and they are resuired for oEmbed Discover You are right that they are for other sites to consume your content, and if you don’t care about it, just remove it. … Read more

using wp_remote_get instead of file_get_contents [duplicate]

If you need to send a JSON response, then there’s a set of functions for that. In case you need that for an AJAX callback: wp_remote_retrieve_response_message() wp_remote_retrieve_response_code() wp_send_json_success() wp_send_json_error() wp_send_json() Would finally be something like that: $request = wp_remote_get( ‘http://example.com’ ); $response = wp_remote_retrieve_body( $request ); if ( ‘OK’ !== wp_remote_retrieve_response_message( $response ) OR 200 … Read more