Create filtered list of posts using JSON data

First, you must allow your custom field be queried from the REST API. Let’s start simple with an example from 1fix.io: function my_allow_meta_query( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( ‘meta_key’, ‘meta_value’ ) ); return $valid_vars; } add_filter( ‘rest_query_vars’, ‘my_allow_meta_query’ ); So, asuming your field name is called “favorite_animal”, for this example, you can … Read more

Full-Ajax Theme: parseJSON error while building a JSON object from a WordPress custom template

Replacing all the specific the_content() functions, or the_ID… etc by the getters equivalent seems to fix the problem. Here the solution: <?php $o = array(); $o[ ‘title’ ] = wp_title( ‘|’, false, ‘right’ ); $o[ ‘type’ ] = ‘single-project’; //ob_start(); $article=”<div id=”container” class=”content-area project-content”>”; $article .= ‘<main id=”main” class=”site-main” role=”main”>’; while (have_posts()) : the_post(); $article … Read more

How to encode post content as JSON?

To output JSON always the function json_encode( $string ). The function is not available on all hosts. Don’t worry, WordPress offers a fallback in wp-includes/compat.php. That’s a wrapper for class Services_JSON::encodeUnsafe() (see wp-includes/class-json.php). If you take a look at the source you’ll see: It’s not a trivial job to encode a string. 🙂 There is … Read more

utf8 encoding in json rest api

The problem is here: ‘excerpt’ => wp_json_encode($post->post_excerpt), That array will get passed through wp_json_encode by the REST API, so it gets double encoded, so remove the wp_json_encode here, and it’ll work out fine, it’s just encoding, it’s not the literal human readable value. E.g. put it in your browsers dev console and output the result, … Read more

Changing the entire control choices using wp.customize with JavaScript

You’re close. You just need to replace changeTheChoices() with set() as this is a method on wp.customize.Setting. See the following which also refactors a bit: wp.customize( ‘setting_category’, ‘setting_font’, function( categorySetting, fontSetting ) { categorySetting.bind( function( category ) { var newChoices = {}; // get new data from JSON using ‘category’ and populate the ‘newChoices’ fontSetting.set( … 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