I'm developing a plugin that relies pretty heavily on AJAX calls. It works well in a single-install version of WordPress, but when I ported it into a Network WP my AJAX functions stopped working (always return 0).
Simplified, my plugin setup looks like this:
// plugin.php
/*
Plugin Name: My plugin
*/
defined( 'ABSPATH' ) or die( 'No direct access.' );
add_action("wp_ajax_test_ajax", "test_ajax");
add_action("wp_ajax_nopriv_test_ajax", "test_ajax");
function test_ajax() {
echo "Works";
die();
}
add_action( 'wp_enqueue_scripts', 'cms_load_head_scripts2' );
function cms_load_head_scripts2() {
$path = get_plugin_path();
wp_enqueue_script('cmsAJAX', $path . 'js/script.js',
array('jquery'), '1.98' );
wp_localize_script( 'cmsAJAX', 'myAjax', array( 'ajaxurl' => get_admin_ajax_path()) );
}
// script.js
(function($) {
$(document).ready( function(){
$.ajax({
type : "post",
dataType : "html",
url : myAjax.ajaxurl,
data : { 'action' : 'test_ajax' },
success : function(response) {
console.log( "success", response );
},
error : function(xhr, ajaxOptions, thrownError) {
}
});
The plugin otherwise works as expected; content loads, functions run etc. It's just the wp_ajax_ actions that don't run.
Through testing, I've determined when I place my add_actions('wp_ajax_
inside the theme's functions.php file, the calls work as expected (even with the ajax call still in the plugin file). I do need them all to be in the plugin though.
I followed admin-ajax.php through and everything is functioning correctly up through the do_action('wp_ajax_
call - no errors here, it just doesn't run my test_ajax
action/function.
After writing this I've figured out that the AJAX calls works perfectly in the plugin if the plugin is activated across the entire network. It'd certainly be possible to only activate this plugin on on or two of the network sites individually though, so I'm still trying to figure out what's going wrong with these ajax calls in a single-site-of-a-network-site plugin.
Also
Here's the two custom get_
functions I used above, they simply return the corresponding paths.
function get_plugin_path(){ return plugin_dir_url(__FILE__); }
function get_admin_ajax_path(){ return ".php"; }
I'm developing a plugin that relies pretty heavily on AJAX calls. It works well in a single-install version of WordPress, but when I ported it into a Network WP my AJAX functions stopped working (always return 0).
Simplified, my plugin setup looks like this:
// plugin.php
/*
Plugin Name: My plugin
*/
defined( 'ABSPATH' ) or die( 'No direct access.' );
add_action("wp_ajax_test_ajax", "test_ajax");
add_action("wp_ajax_nopriv_test_ajax", "test_ajax");
function test_ajax() {
echo "Works";
die();
}
add_action( 'wp_enqueue_scripts', 'cms_load_head_scripts2' );
function cms_load_head_scripts2() {
$path = get_plugin_path();
wp_enqueue_script('cmsAJAX', $path . 'js/script.js',
array('jquery'), '1.98' );
wp_localize_script( 'cmsAJAX', 'myAjax', array( 'ajaxurl' => get_admin_ajax_path()) );
}
// script.js
(function($) {
$(document).ready( function(){
$.ajax({
type : "post",
dataType : "html",
url : myAjax.ajaxurl,
data : { 'action' : 'test_ajax' },
success : function(response) {
console.log( "success", response );
},
error : function(xhr, ajaxOptions, thrownError) {
}
});
The plugin otherwise works as expected; content loads, functions run etc. It's just the wp_ajax_ actions that don't run.
Through testing, I've determined when I place my add_actions('wp_ajax_
inside the theme's functions.php file, the calls work as expected (even with the ajax call still in the plugin file). I do need them all to be in the plugin though.
I followed admin-ajax.php through and everything is functioning correctly up through the do_action('wp_ajax_
call - no errors here, it just doesn't run my test_ajax
action/function.
After writing this I've figured out that the AJAX calls works perfectly in the plugin if the plugin is activated across the entire network. It'd certainly be possible to only activate this plugin on on or two of the network sites individually though, so I'm still trying to figure out what's going wrong with these ajax calls in a single-site-of-a-network-site plugin.
Also
Here's the two custom get_
functions I used above, they simply return the corresponding paths.
function get_plugin_path(){ return plugin_dir_url(__FILE__); }
function get_admin_ajax_path(){ return "http://domain/wp-admin/admin-ajax.php"; }
Share
Improve this question
edited Jan 12, 2016 at 21:11
DACrosby
asked Jan 12, 2016 at 11:17
DACrosbyDACrosby
4154 silver badges15 bronze badges
4
|
2 Answers
Reset to default 1Instead of using get_admin_ajax_path()
use self_admin_url( 'admin-ajax.php' )
which retrieves the URL to the ajax address for either the current site or the network.
If you have a multisite installation, better to use the inbuilt WordPress core function network_admin_url to retrieve the admin-ajax.php url
get_admin_ajax_path()
? I've never seen that function before, it is written by you or it is from WordPress core? Can you post the code of that function? – cybmeta Commented Jan 12, 2016 at 11:52get_plugin_path()
– TheDeadMedic Commented Jan 12, 2016 at 13:55admin_url( 'admin-ajax.php' )
in sigle site instalattions,network_admin_url( 'admin-ajax.php' )
to get the ajax url of current site in multisite enviroment orget_admin_url()
function if you need to control exactly which site of the network should be used (not necessarily the current site). – cybmeta Commented Jan 14, 2016 at 7:37