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.

I'm trying to ajaxify my Wordpress theme and I use the method described here and I'm now trying get_the_content of post via functions.php. Using jQuery, when I do alert(data) I get the 'title' echo but not the content of the existing post I want (returns 0).

The jQuery part

$('.ajaxed,.ajaxed a,.menu-item-home a,.menu-item-object-page a').live('click', function(event) {
        event.preventDefault();
        var link = $(this).attr('href');
        var toRemove = MySettings.url;
        var rewritepath = link.replace(toRemove,'');
        var handler = function(data) {
            $('title').html($('title', data).html());
            $('#primary').html($('#primary', data).html());
            $('#primary').hide().fadeIn('slow');
            $.address.title(/>([^<]*)<\/title/.exec(data)[1]);
        };
        $.post(ajax_object.ajaxurl, {
            action: 'ajax_action',
            post_id: $(this).find('input.post_id').attr('value')
        },function(data) {
            alert(data.title);
            alert(data.content);
        });
        $.address.state(MySettings.path).crawlable(true).value(rewritepath);
        return false;
    });

The functions.php part

<?php
function javascripts() {
    if( !is_admin()){
        $blogurl = get_bloginfo('url');
        $thumbnail_width = get_option('thumbnail_size_w');
        $thumbnail_height = get_option('thumbnail_size_h');
        $path = parse_url(get_bloginfo('siteurl'), PHP_URL_PATH);
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js';
        wp_deregister_script('jquery');
        if (get_transient('google_jquery') == true) {       
            wp_register_script('jquery', $url, array(), null, true);
        } 
        else {
            $resp = wp_remote_head($url);
            if (!is_wp_error($resp) && 200 == $resp['response']['code']) {
                set_transient('google_jquery', true, 60 * 5);
                wp_register_script('jquery', $url, array(), null, true);
            } 
            else {
                set_transient('google_jquery', false, 60 * 5);
                $url = get_bloginfo('wpurl') . '/wp-includes/js/jquery/jquery.js';
                wp_register_script('jquery', $url, array(), '1.7', true);
            }
        }
        wp_enqueue_script('plugins.js', get_bloginfo('template_directory') . "/js/plugins.js" , array('jquery'));
        wp_enqueue_script('ajax-script', get_bloginfo('template_directory') . "/js/scripts.js", array('jquery'));
        wp_localize_script('ajax-script', 'ajax_object', array('ajaxurl' => admin_url( 'admin-ajax.php' )));
        wp_localize_script('jquery', 'MySettings', array('width' => $thumbnail_width,'height' => $thumbnail_height,'url' => $blogurl,'path' => $path));
    }
}
add_action('wp_enqueue_scripts', 'javascripts');
add_action('wp_ajax_ajax_action', 'ajax_action_stuff'); // ajax for logged in users
add_action('wp_ajax_nopriv_ajax_action', 'ajax_action_stuff'); // ajax for not logged in users
function ajax_action_stuff() {
    $post_id = $_POST['post_id'];

    //Query the post and set-up data
    $post = get_post($post_id);
    setup_postdata();

    update_post_meta($post_id, 'post_key', 'meta_value');

    //Return response as an array. This will give us data.title and data.content browser-side.
    $response = array();
    $response['title'] = get_the_title();
    $response['content'] = get_the_content();

    echo json_encode($response);
    exit;
}
?>

What am I doing wrong? Thanks

share|improve this question
    
»... and I use the method described here ...« Described where? –  kaiser Feb 20 '12 at 17:49
    
Also: Where's your add_action to wp_ajax(_nopriv)_*? –  kaiser Feb 20 '12 at 17:50
1  
Corrected ;-) thanks –  Gab Feb 20 '12 at 19:12
add comment

2 Answers

get_the_content() must be used inside the loop, or otherwise you'll need to use setup_postdata(); providing the post object.

Try the following:

function ajax_action_stuff() {
    $post_id = $_POST['post_id'];

    //Query the post and set-up data
    $post = get_post($post_id);
    setup_postdata( $post );

    update_post_meta($post_id, 'post_key', 'meta_value');

    //Return response as an array. This will give us data.title and data.content browser-side.
    $response = array()
    $response['title'] = get_the_title();
    $response['content'] = get_the_content();

    echo json_encode($response);
    exit;
}

This has not been tested

share|improve this answer
    
I'm sorry... I erase the previous comment since I did a mistake. –  Gab Feb 20 '12 at 19:35
    
alert box shows: <br /> <b>Warning</b>: Missing argument 1 for setup_postdata() ... etc –  Gab Feb 20 '12 at 19:36
    
corrected setup_postdata(); to setup_postdata($post); ... Will test further more and come back. –  Gab Feb 20 '12 at 19:38
    
@Gab please take a look at the codex. As Stephen Harris said: Not tested, so there might be missing stuff. (Now edited). –  kaiser Feb 20 '12 at 19:38
add comment
up vote 0 down vote accepted

I got a solution here in another post I made about the same problem I had.

share|improve this answer
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.