admin管理员组文章数量:1026989
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
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
-
1
What is
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:52 -
Same for
get_plugin_path()
– TheDeadMedic Commented Jan 12, 2016 at 13:55 - Both of those functions are custom - they simply return the string path of the admin-ajax file and plugin directory. I had some challenges getting the ajax path to load right in multisite so just hardcoded as a temp solution, put it in a function so it's easy to fix in the future. I'm adding them to the original post. – DACrosby Commented Jan 12, 2016 at 21:09
-
Try to get the admin-ajax URL correctly. Instead of hard-coding it, you should use
admin_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
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
本文标签: multisiteaddaction wpajax not loading in plugin file WP Network
版权声明:本文标题:multisite - add_action wp_ajax_ not loading in plugin file WP Network 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662254a2161961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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