WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently developed a small plugin for my site, that uses AJAX, and now I am trying to implement the same AJAX techniques for the same site's templates, but I keep getting -1 as the result, here is my code,


    //Front end AJAX functions

    function eu_custom_query(){
        global $post;
        global $wpdb;

        echo "yo";


function enque_template_scripts() {

    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://code.jquery.com/jquery-1.6.2.js');
    wp_enqueue_script( 'jquery' );

    // embed the javascript file that makes the AJAX request
    wp_register_script( 'scripts.js', get_bloginfo('template_directory').'/scripts/scripts.js');
    wp_enqueue_script( 'scripts.js' );

    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'scripts.js', 'wp_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); 


    add_action('wp_ajax_custom_query', 'eu_custom_query');
    add_action('wp_ajax_nopriv_custom_query', 'eu_custom_query');
    add_action('get_header', 'enque_template_scripts');


            $.post(wp_ajax.ajaxurl, { action: 'eu_custom_query' }, function(data){

I'm using Wordpress version 3.2.1

Any ideas on what I'm doing wrong?

share|improve this question
up vote 3 down vote accepted

You are passing 'eu_custom_query' as the action in your AJAX. It should be 'custom_query'.

You decide the action name, when you add the action i.e. 'wp_ajax_my_action' would be called 'my_action'

You are confusing it with the callback method. Hope that makes sense.

p.s. to fix your code change:

add_action('wp_ajax_custom_query', 'eu_custom_query');
add_action('wp_ajax_nopriv_custom_query', 'eu_custom_query');


add_action('wp_ajax_eu_custom_query', 'eu_custom_query');
add_action('wp_ajax_nopriv_eu_custom_query', 'eu_custom_query');
share|improve this answer
Oooh my word! I can't believe I missed that! Thanks for pointing it out, that was the problem. – Odyss3us Jul 18 '11 at 12:44
You just needed a fresh pair of eyes. :) – Cristian Jul 18 '11 at 12:44

I see two issues:


WordPress requires noConflict wrappers for jQuery. So this:


...needs to be this:

jQuery(document).ready(function($) {

...so that $ will work properly inside the function.

Blanket replacement of core-bundled script

Also, you are nakedly replacing the core-bundled jQuery, which you should never do, or you will risk breaking things in the WP-Admin backend.

Any such code must be wrapped inside an if ( ! is_admin() ) conditional:

if ( ! is_admin() ) {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://code.jquery.com/jquery-1.6.2.js');
    wp_enqueue_script( 'jquery' );
share|improve this answer
Thank you for the tips, they are greatly appreciated! – Odyss3us Jul 18 '11 at 12:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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