Get posts with ajax

Option 1 The simple way – return all the posts in JSON format and loop through them in JavaScript PHP: function get_ajax_posts() { // Query Arguments $args = array( ‘post_type’ => array(‘products’), ‘post_status’ => array(‘publish’), ‘posts_per_page’ => 40, ‘nopaging’ => true, ‘order’ => ‘DESC’, ‘orderby’ => ‘date’, ‘cat’ => 1, ); // The Query $ajaxposts … Read more

Multiple ajax nonce requests

That is basically the way WordPress does it and pretty much the only way to do it, simply have your receiving PHP function create a new nonce add send it back with your response, then just update the value on your JS before the next round.

Including WordPress in RESTful API

The credit for this answer really goes to @Wyck who correctly identified the source of the problem. Thank you very much. The reason for the problem is: There is a variance between an import statement in the mainline code versus within a function. In WP or in in any of the plugins if variables aren’t … Read more

Is it safe to assume that a nonce may be validated more than once?

1, the nonce lifetime is about 24 hours by default actually. take a look at wp_verify_nonce function. To be more accurate, the lifetime is controlled by filter apply_filters( ‘nonce_life’, DAY_IN_SECONDS ); 2, if the lifetime value makes you doubt if it is “an implementation side-effect”, you may want to add_filter(‘nonce_life’,create_function(‘$v’, ‘return 60*5;’)); to shorten the … Read more

Call to undefined function add_action()

WordPress has a native AJAX file that should do all that you want, and this will ensure that all native WP functions are included. If you require additional functionality from ‘framework/AsyncAction.php’, you can include_once(), as you do with ‘SettingsController.php’. See the Codex for more information and examples – AJAX in Plugins If you wish to … Read more

Stop admin-ajax?

The first thing admin-ajax.php does is define DOING_AJAX. Then, it loads wp-load.php. It does some other stuff, and the first thing it comes across that you have control over is wp-config.php. So if you want to stop all ajax, you can add to following to your wp-config.php file. if( defined( ‘DOING_AJAX’ ) && DOING_AJAX ) … Read more

admin-ajax.php vs Custom Page Template for Ajax Requests

First, the obvious drawback to the first method is that it depends on your specific page, template, and permalink structure to all work correctly. Using admin-ajax.php will work correctly in any context, theme or plugin, where proper WordPress best practices are followed. The less obvious drawback to the first method is that it uses more … Read more