Placing assets for external use

I have used symbolic links for convenience for awhile on a dev server but they are not 100% reliable as some PHP functions will return the filesystem path of the target instead of the path to the symlink itself. The most notable example (though not really a function) is the magic constant __FILE__. (Also https://bugs.php.net/bug.php?id=46260) … Read more

esc_attr() corrupts json values

Normally, when one want to put strings to be used in javascript, esc_js is the right function, not esc_attr. The problem is that esc_js, according to docs: Escapes text strings for echoing in JS (bold mine). So, using with esc_js you obtain a string that can be safely echoed in js, not parsed: it’s not … Read more

WP REST API format response

Since you’re filtering with the rest_prepare_{post_type} filter, you could restrict it to the WP_REST_Posts_Controller::get_items() callback, with the rest_{post_type}_query filter: add_filter( ‘rest_post_query’, function( $args ) { add_filter( ‘rest_prepare_post’, ‘api_remove_extra_data’, 12, 3 ); return $args; } ); where the post type is post. Note that in general we always want to return the filter value and I … Read more

Adding JSON Structured Data to WordPress

If you’re creating the theme yourself, you can always use Schema Microata markup directly in the theme itself. As we can see in the below example, adding extra attributes to your HTML can make it Schema compliant. In this instance, we are using itemscope, itemtype and itemprop: Taken from http://schema.org/docs/gs.html#microdata_how <div itemscope itemtype =”http://schema.org/Movie”> <h1 … Read more

How can I cache WordPress Rest API Response

You should create a new instance from WP_REST_Response to set the Cache-Control value. <?php register_rest_route(‘wp/v2’, ‘/your_endpoint’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘function_callback’, )); function function_callback($data) { $response = array( 1,2,3,4,5,6,7,8 ); $result = new WP_REST_Response($response, 200); // Set headers. $result->set_headers(array(‘Cache-Control’ => ‘max-age=3600’)); return $result; } Click here to get more info about directives.

Most performant way of fetching remote API data?

The most performant way of fetching remote API data is not fetching it at all. Thus, use Transients API or WP Object Cache to save your computed results for future use and avoid calling external API (and further computations) on every subsequent request. Additionally, fetching, invalidating and regeneration of this data can be done in … Read more

WordPress REST API – Modify JSON before importing

Using the rest_pre_dispatch hook is probably the most straightforward way to go, but you need to be careful about putting your new request data back into the WP_REST_Request object properly. You were reassigning the WP_REST_Request object to an array of request data. Those changes are not persisted because the parameter is not passed by reference. … Read more

How to handle shortcodes when using the JSON API

You can add your own AJAX API for do_shortcode. Add this to a suitable location (i.e. functions.php or a plugin): add_action(‘wp_ajax_doshortcode’, ‘ajax_doshortcode’); function doshortcode() { echo do_shortcode($_POST[‘text’]); die(); // this is required to return a proper result } And this to your Javascript: $.ajax({ url : ajaxurl, data : { action : ‘doshortcode’, text : … Read more