Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

        success: function(data) {
            console.log("Succes");
            row.before('<div id="case" class="twelve columns omega alpha"></div>');
            $("#case").slideUp(1);
            $("#case").html(data).slideDown(speed);

            $("#header, #featured, .row").animate({'opacity': halfOpacity}, speed);
            return false;
        },
        error: function() {
            console.log("Error");
            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;
    exit;
}
share|improve this question
    
By 'the URL doesnt become accessable' - are you after a reliable method of getting an url pointing to the admin-ajax.php page? –  Stephen Harris Apr 30 '12 at 11:26
add comment

1 Answer

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"> 
//<![CDATA[
    ajaxurl = '<?php echo(admin_url('admin-ajax.php')); ?>';
//]]>
</script>

<?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?

share|improve this answer
    
It only defines ajaxurl on the admin side. –  Stephen Harris Apr 30 '12 at 11:21
    
You're right. I've edited the answer to avoid confusion. –  tbuteler Apr 30 '12 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 Apr 30 '12 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 May 2 '12 at 6:50
add comment

Your Answer

 
discard

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.