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

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

Get Previous & Next posts by Post ID

Take a look at get_previous_post() and get_next_post() and you’ll see they both use the get_adjacent_post() to find the previous or next post. Let’s say you want to fetch the ID of the immediately previous post based on the current post’s ID. This is what you’d do: function get_previous_post_id( $post_id ) { // Get a global … Read more

Is there a JavaScript API? How to access public and private data in JS?

TL;DR There is no JavaScript API in the WordPress core and no one is planned, but actually, there is no need of it. Backend First of all let’s say that, regarding the backend, some useful information can be fetched from already present JavaScript global variables (WordPress loves all global flavors). E.g. ajaxurl for the admin-ajax.php … Read more

Making my AJAX powered WordPress Crawlable

Specifically AVOID using “hashbang” (“!#”) in order to make AJAX powered WordPress sites crawlable. You really don’t want to use the “hashbang” method on a WordPress site. The “!#” is more like a hacky patch for sites that cannot provide a static analog to it’s AJAX version. Its use in general is not recommended by … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)