Retrieve list of taxonomies in json

Your problem is that your trying to access taxonomy data before it is registered. This doesn’t work: add_action(‘init’, ‘json_handler’); function json_handler(){ $categories = get_terms( ‘my_cat’, ‘orderby=count&hide_empty=0’ ); if( ! is_wp_error( $categories ) ) { // encode the $categories array as json print_r( json_encode( $categories ) ); } } add_action(‘init’, ‘create_stores_nonhierarchical_taxonomy’); function create_stores_nonhierarchical_taxonomy() { // Labels … Read more

Can’t add classes using jQuery from a JSON string with get_body_class()

get_body_class() is going to return an array of classes, which is then (per code not published in your question) JSON encoded into that comma separated string. While you can manipulate that in JavaScript, the easiest thing to do is implode the string before encoding: $c = get_body_class(‘project’); $c = implode($c,’ ‘); You should have a … Read more

Call Ajax URL in Plugin

Put below code in _construct() function and change action name to get_product_serial_callback :- add_action( ‘wp_ajax_get_product_serial_callback’, array($this,’get_product_serial_callback’) ); add_action( ‘wp_ajax_nopriv_get_product_serial_callback’, array($this,’get_product_serial_callback’ ));

WP Rest API and json_decode()

wp_remote_get return array of results containing keys body, headers, response etc depending on arguments. And json_decode only accept JSON string. So correct your code in this way //return if not an error if ( ! is_wp_error( $response ) ) { //decode and return return json_decode( wp_remote_retrieve_body( $response ) ); } Ref: Return Values of wp_remote_get … Read more

Redirects based on a JSON file

The way I do it. Note there may be a more straightforward way. Step 1 Add a custom query_varlike this to record the redirect from/to variables function my_custom_query_vars($vars){ //this allows you to store custom variables with rediect_from and rediect_to in the url $vars[] = ‘redirect_from’; $vars[] = ‘redirect_to’; return $vars; } add_filter( ‘query_vars’, ‘my_custom_query_vars’ ); … Read more

How to get more data of a post by wp_query

WordPress has many built in functions that allow you to get whatever data you have added to your posts, whether it standard or custom. Example below: if ($query->have_posts()) { while ($query->have_posts()) { $posts = $query->get_posts(); $array[‘title’] = get_the_title(); $array[‘permalink’] = get_the_permalink(); $array[‘content’] = get_the_content(); $array[‘post_date’] = get_the_date(); if (has_post_thumbnail()) { $array[‘feat_image_url’] = wp_get_attachment_url(get_post_thumbnail_id()); } $array[‘custom_meta_1’] … Read more