WordPress Ajax Data Security

There are a few things you can do to make more secure: First the Ajax call it self should be made with a WordPress nonce like you said: <script type=”text/javascript” > jQuery(document).ready(function($) { var data = { action: ‘ACTION_NAME’, Whatever_data: 1234, _ajax_nonce: <?php echo wp_create_nonce( ‘my_ajax_nonce’ ); ?> }; $.post(ajaxurl, data, function(response) { alert(‘Got this … Read more

How to pass data parameter to ajax action function handler

When you use jQuery.post(), the data is sent as regular $_POST arguments. So this JavaScript … var data = { action: ‘load_post’, foo: ‘bar’ }; … is available in your callback function per: $action = $_POST[‘action’]; $foo = $_POST[‘foo’]; // bar And when you are using jQuery.get(), the data is in $_GET. You can also … Read more

Ajax in a settings page (update_option is undefined)

Don’t send AJAX to your PHP file directly. Instead, use WordPress’ built-in AJAX functions 1). You can register an AJAX callback from your plugin, and WordPress will route requests to your plugin file for you. For example, this code will register a callback called “wpa_49691”: add_action( ‘wp_ajax_wpa_49691’, ‘wpa_49691_callback’ ); add_action( ‘wp_ajax_nopriv_wpa_49691’, ‘wpa_49691_callback’ ); function wpa_49691_callback() … Read more

Using Ajax with a Class file

The errors I’ve spotted in your code: One was pointed by @helgatheviking in a comment: the Ajax callback has to be a public method, not private. Not sure how are you initializing this class, but wp_enqueue_script&_style (in singular) has to be encapsulated inside a wp_enqueue_scripts hook (WP_DEBUG dumps a notice). You can drop the & … Read more

Using Backbone with the WordPress AJAX API

You need to override the Backbone.sync function to change the URL used for the AJAX call. You can learn from the plugin wp-backbone does this and more. Below you can see from it how it changes all the actions into either POST or GET, create the parameters, add the action parameter and more. (function($) { … Read more

How to save dismissable notice state in WP 4.2?

There’s no need for a library, it’s fairly straightforward to accomplish. If you are coding your own plugin (which it appears you are), then you can do this in a fairly lightweight way: A slightly modified version of the notice, which checks if it’s been dismissed before displaying, and stores the “type” of notice in … Read more

failed to load wp-admin/admin-ajax.php

Yes finally the problem was that the hosting provider had blocked the admin-ajax.php file saying that this file was receiving too many request, and requests to this file bypasses cache , hence it was causing problems on server 🙂 So now i’ll have to ‘convince’ them to turn it back on. Thank you all for … Read more

How can I fetch loop of post titles via AJAX?

you can wrap your function and hook it to ajax call like this: //if you want only logged in users to access this function use this hook add_action(‘wp_ajax_more_links’, ‘my_AJAX_more_links_function’); //if you want none logged in users to access this function use this hook add_action(‘wp_ajax_nopriv_more_links’, ‘my_AJAX_more_links_function’); function my_AJAX_more_links_function(){ check_ajax_referer(‘more_links’); $success_response = new WP_Ajax_Response(); $args = array(‘posts_per_page’ … Read more