Send data to 3rd party api with wp_remote_post on wp_login

The ‘body’ needs to be an array, not including the ‘json_encode($user)’ piece. $response = wp_remote_post( ‘myapp.com/endpoint’, array( ‘method’ => ‘POST’, ‘headers’ => array(‘Content-Type’ => ‘application/json; charset=utf-8’), ‘body’ => $user ) ); I have this in my function since I also had issues with the body being an object: if (is_object($user) && !is_array($user)) $user = json_decode(json_encode($user), … Read more

Unknown language json files

what are these language files for? (I guess it is for localizing javascript?) You guessed right. The JSONs are for use specifically with the new wp_set_script_translations function in WordPress 5. is it necessary to add/commit/push them to the live server? They are used at runtime, just like .mo files are. So yes, add them to … 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

Setting default font family with theme.json

I had the same problem and I find out how to solve it: For each “fontFamily” inside “fontFamilies” array, you get a custom variable. From your example You can use both variables on your stylesheet or in theme.json: –wp–preset–font-family–helvetica-arial –wp–preset–font-family–ubuntu-sansserif And then you call the font like this: { “version”:1, “settings”: { “typography”: { “fontFamilies”: … Read more

How to get the path to current theme, but from a JS file?

You can use wp_localize_script to pass an object with the url to load before the file. and then in the file you can use it like testpage_obj.json_data wp_register_script(‘testpage’, get_stylesheet_directory_uri() . ‘/js/testscript.js’, array(‘jquery’), ”, true); wp_localize_script( ‘testpage’, ‘testpage_obj’, array( ‘json_data’ => get_stylesheet_directory_uri() . ‘/js/mydata.json’ ) ); wp_enqueue_script(‘testpage’); In the JS file jQuery.getJSON(testpage_obj.json_data)

WordPress JSON output

If you are using PHP 5.2+ you’re best bet is to just make a PHP array or object and use json_encode(). UPDATED: $cats = get_categories(); $output = array(‘categories’ => array()); foreach ($cats as $cat) { $cat_output = array( ‘cat_id’ => $cat->term_id, ‘cat_name’ => $cat->name, ‘posts’ => array(), ); // should be able to use -1 … Read more

I want to create a custom slug in WordPress and output JSON. How do I do this?

You can use pre_get_posts as suggested but simply check the global $wp object for the request url.. like so: add_action( ‘pre_get_posts’, function ($query ){ global $wp; if ( !is_admin() && $query->is_main_query() ) { if ($wp->request == ‘cats’){ header(‘Content-Type: application/json’); $cats = array(‘Hobbes’, ‘Simba’, ‘Grumpy Cat’); echo json_encode($cats); exit; } } }); (PHP 5.3+ in this … 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