Skip to main content
deleted 82 characters in body
Source Link
david.binda
  • 3.4k
  • 1
  • 20
  • 16

ajaxurl is set only in admin. If this is a frontend ajax (looks like it due to nopriv), you have to localize the script on enqueueing:

add_action( 'admin_enqueue_scripts''wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    if( 'index.php' != $hook ) return;  // Only applies to dashboard panel
        
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

In the JS file, you can reach the ajax_url as a variable of ajax_object:

jQuery.ajax(
        ajax_object.ajax_url, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

See http://codex.wordpress.org/AJAX_in_Plugins

ajaxurl is set only in admin. If this is a frontend ajax (looks like it due to nopriv), you have to localize the script on enqueueing:

add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    if( 'index.php' != $hook ) return;  // Only applies to dashboard panel
        
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

In the JS file, you can reach the ajax_url as a variable of ajax_object:

jQuery.ajax(
        ajax_object.ajax_url, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

See http://codex.wordpress.org/AJAX_in_Plugins

ajaxurl is set only in admin. If this is a frontend ajax (looks like it due to nopriv), you have to localize the script on enqueueing:

add_action( 'wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
        
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

In the JS file, you can reach the ajax_url as a variable of ajax_object:

jQuery.ajax(
        ajax_object.ajax_url, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

See http://codex.wordpress.org/AJAX_in_Plugins

Source Link
david.binda
  • 3.4k
  • 1
  • 20
  • 16

ajaxurl is set only in admin. If this is a frontend ajax (looks like it due to nopriv), you have to localize the script on enqueueing:

add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    if( 'index.php' != $hook ) return;  // Only applies to dashboard panel
        
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery'));

    // in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

In the JS file, you can reach the ajax_url as a variable of ajax_object:

jQuery.ajax(
        ajax_object.ajax_url, {
        action: 'lu_ajax',
        status: 'enabled'
        },
        success: function (a) {
        alert(a)
        }

See http://codex.wordpress.org/AJAX_in_Plugins