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";

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(){

        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.


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.com/wp-admin/admin-ajax.php"; }
  • 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() 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 or get_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 2


Instead 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.