Hot answers tagged

9

serialize representation can be stored in text and reversed JSON representation can be stored in text but cannot always be precisely reversed Run this example: $query = new WP_Query(); var_dump( $query ); var_dump( unserialize( serialize( $query ) ) ); var_dump( json_decode( json_encode( $query ) ) ); After going through serialize accurate WP_Query ...


9

TL;DR Yes, WordPress can certainly act as a backend for a mobile app. Yes, a page can act as a rest endpoint / interface. No, a theme template is not the right territory for the logic. Write your own plugin. Pointers I find it hard to believe that no one else has done this. I, for one, have. More than once. And I'm near certain I'm not alone. "No ...


9

I don't think using React.js without Node.js (or at least V8 or rhino etc) counts as isomorphic, as isomorphic means that you are building JavaScript to run in the browser AND on the server. Specifically, using WordPress certainly means you aren't doing isomorphic javascript (its PHP software). What you could do is use WordPress as a REST API server, and ...


8

It did appear that the json wasn't being cached by wp-super-cache, but we decided to take a different approach. By using the transient api, we were able to do a faux-cache on all json, and drastically reduce the taxing of the database. Then on the ajax side of things, we are caching the html that is created from this semi-cached json. Things are super speedy!...


7

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(), ); ...


7

Here is some quick first draft code for populating a dropdown from the Google Font API, I do not know about the options framework so this will not deal with that. 1. Get an API Access Key from Google Your request will need a valid key, you can follow the instruction here on how to get one: https://developers.google.com/webfonts/docs/developer_api It's ...


7

Sorry for answering my own question but it may help some other devs too. I created this additional filter 'json_query_var-meta_query'that returns the necessary arguments. function adjustQrry($data){ $args = array(); $args['relation'] = 'AND'; foreach ($data as $key=>$value) { if ( 'relation' === $key ) { $args['relation']...


6

I took a look at the WordPress app for Windows 8. It looks as though it is strictly built for WordPress.com, not self-hosted blogs. The apps available for Android, iOS, and Windows Phone on the other hand actually support both self-hosted installations and WordPress.com. Even though you integrated some WordPress.com services into your blog via JetPack, it ...


6

WP Super Cache examines your WordPress site's pages for some HTML tags before it caches them. Your pages most probably don't have </html> tag (common issue), in that case, try adding something like //</html> -- that's a workaround, and WP Super Cache should then generate cached versions of your pages. Why does WP Super Cache do it like that? ...


6

My idea was to schedule this function, so it will run once every day or so. After some more research I should be able to fix this (using this post). Note that WP_Cron setted to daily is impossible to control exactly at what time the creation happen. If no one visit your site on night the first user on morning will experience a very long page loading ...


6

wp_send_json() handles all parts of returning content in an AJAX call. First off, it sets the content type of the returned content to application/json with the proper charset. Secondly, it automatically calls wp_die() after sending the JSON result, which is necessary in an AJAX call in WordPress. You could consider using wp_send_json_success() for ...


6

This is the approach to add a rewrite rule for swapping out the template that WP wants to render (usually the 404 template) with something else. In your case you're just wanting to die with some JSON data so there are a bunch of different ways to do this. This is the "by the book" approach to adding a rewrite rule instead of a hack to check the URL on your ...


5

There is an elegant way to handle this! Pass the JSON encoded string through wp_slash(). That function will escape the leading slash of each encoded unicode character, which will prevent update_metadata() from stripping them.


5

Depending on what kind of HTML you're expecting, there are different tools you can use: esc_html() escapes entire HTML blocks so you don't end up with breaking characters in your JSON object literals. esc_html_e() escapes (as above) and translates the string if you're concerned about localization in that context. wp_kses() will parse the HTML string and ...


5

Ok, found. I spent hours on this but I managed to find the solution. The command I was looking for was $rss->enable_order_by_date(false);. So you should set (for benefit of the community): <?php /* include the required file */ include_once(ABSPATH . WPINC . '/feed.php'); /* specify the source feed */ $rss = fetch_feed('FEED_URL'); ...


5

I will give you a small answer to your update, doing this with the WP API. The API have the possibilities to use the WP_Query like also in core, but about the get parameters in the url. A URL to pull content from Post Status would look like this: http://example.com/wp-json/posts To pull content with WP_Query parameters you’re used to, you could do it ...


4

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' =...


4

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 = ...


4

There are two parts to the solution here. You need to use a JSON API custom controller In your custom controller, you'll need to decide how to pass the meta_query data structure Depending on how robust you need this to be, you could use a variety of approaches. Here is the maximalist approach, that will allow any kind of meta_query, encoding the ...


4

If possible, only the examples shown in internet is: qod_remove_extra_data function ($ 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 [...


3

The problem will likely be a (very slow) callback that is attached to your get_the_excerpt() function. To inspect the attached callbacks, just inspect the global: // Best hooked to `shutdown` echo '<pre>'.var_export( $GLOBALS['wp_filters']['get_the_excerpt'], true ).'</pre>'; Then get rid of all those callbacks.


3

The AJAX request would query for the dates it requires to display. With Lazy fetching set, if you switch from month to week or day views it won't bother making another request, since it doesn't need to. This is outlined in the fullcalendar documentation. As for using this method, see this question on how to obtain the admin-ajax.php url properly. Next, you ...


3

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 : { ...


3

Doesn't look like there's any way to avoid it. The update_metadata() function, which is ultimately responsible for saving the meta, explicitly runs a stripslashes_deep() on the meta value. This function will even strip slashes from array elements, if the value were an array. Theres a filter that's run AFTER that called sanitize_meta, which you could hook ...


3

For the record, I'm not sure why anyone would use the JetPack JSON API as it currently works for a self hosted site. If you're doing AJAX and JSON, the whole point is to get back some JSON data FAST. Using jetpack involves an unnecessary call to wordpress.com, which then does a call to your own self hosted blog. Using the wonderful JSON API plugin, you ...


3

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) That can cause ...


3

@BODA82 answer helped, but eventually I realized that I should have replaced responseText with responseJSON method in my javascript code. In the example below I was storing the ajax response results in a variable. I didn't know there was a specific method to get response in JSON. In such way the object/array with get_posts() results is returned correctly and ...


3

it seems the latter only accepts x-www-form-urlencoded That's not completely true. WordPress admin-ajax.php takes the action from $_REQUEST['action'] and $_REQUEST is equal to: array_merge($_POST, $_GET); But what many people don't realize is that $_GET in PHP is not the data was sent to page using HTTP GET method, in fact, you can use whatever HTTP ...


3

The easiest way I can think of (and the only one) is create a page with the permalink you're indicating, create a custom template in your theme folder, let's say it's json_output.php with the following content: <?php /* * Template Name: JSON Output */ header('Content-Type: application/json'); $cats = array('Hobbes', 'Simba', 'Grumpy Cat'); echo ...


3

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('...



Only top voted, non community-wiki answers of a minimum length are eligible