Im trying to create an ajax call when I click a link, the link is the post thumbnail, and the result should show the post content.

When I use admin-ajax.php it works fine, but the problem then is that the URL doesnt become accessable.

This is my ajaxcall

        //url: admin_url +  '/wp-admin/admin-ajax.php',
        url: href + '?action=ajax',
        dataType: 'json',
        data: {
            //action: 'ajax',
            post_id: post_id

        success: function(data) {
            row.before('<div id="case" class="twelve columns omega alpha"></div>');

            $("#header, #featured, .row").animate({'opacity': halfOpacity}, speed);
            return false;
        error: function() {
            window.location = href;

And this is the php in the functions.php

  function ajax() {

    global $wpdb;
    global $post;
    $id = $_GET['post_id'];

    $querystr = "
        SELECT $wpdb->posts.*
        FROM $wpdb->posts
        WHERE $wpdb->posts.ID = $id

    $post = $wpdb->get_results($querystr);

    $response = $post[0]->post_content;
    $response = json_encode($response);

    header( "Content-Type: application/json" );
    echo $response;
  • By 'the URL doesnt become accessable' - are you after a reliable method of getting an url pointing to the admin-ajax.php page? Commented Apr 30, 2012 at 11:26

1 Answer 1


Regarding the jQuery part:

Everything looks OK, except for the fact that we have no way of knowing which URL you are ultimately pointing to. You could see the URL, the headers you're sending and the server's response by using Firebug or WebKit's developer tools in Chrome or Safari, and editing the code accordingly.

Since I use AJAX a lot, I find it easier to just put this right before the closing tag on my header.php:

<?php if(!is_admin()) : ?>

<script type="text/javascript"> 
    ajaxurl = '<?php echo(admin_url('admin-ajax.php')); ?>';

<?php endif; ?>

This way, I have a global ajaxurl Javascript variable which will always point to the correct AJAX url.

My bet is that the problem lies on the PHP part, though. You're missing the lines that actually register your PHP function as a WordPress AJAX action. Without them, WordPress can never guess that your ajax() function is actually an AJAX function.

Put this on your functions.php:

add_action('wp_ajax_nopriv_my_custom_callback', 'my_custom_callback');
add_action('wp_ajax_my_custom_callback', 'my_custom_callback');

The logic here is that WordPress will check for the action parameter when inside admin-ajax.php, and trigger the action wp_ajax_{action}, which is, in turn, associated with the callback my_custom_callback. The first line (the one with the nopriv bit), is optional, and you should only use it if you want to provide that AJAX functionality to non-logged-in users.

In your specific case (though I'd strongly recommend something more unique and descriptive than ajax as a function name), it would look like this:

add_action('wp_ajax_nopriv_ajax', 'ajax');
add_action('wp_ajax_ajax', 'ajax');

Let me know how it goes, OK?

  • It only defines ajaxurl on the admin side. Commented Apr 30, 2012 at 11:21
  • You're right. I've edited the answer to avoid confusion. Commented Apr 30, 2012 at 11:27
  • 1
    Rather than adding ajaxurl to a template file, just use wp_localize_script() (Assuming that you have properly enqueued your JS script via wp_enqueue_script())
    – shabushabu
    Commented Apr 30, 2012 at 11:54
  • That works as I said, but the problem with the admin-ajax is that when I click the post thumbnail the content of that post should slide up and the url should change to whatever the postname is, and if I go directly to that post the content should already be visible.
    – Jonas
    Commented May 2, 2012 at 6:50

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.