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.

For a project, i've to improve SEO perf and one of this optimization is to load the sidebar with an ajax request.

I've looking for this solution through the documentation, source code, plugins repository without success.

So i need your help and advices. Is it possible to do this ? If yes, what is the best way ?

Thanks.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I didn't test anything, but I imagine it would be something like this:

As you must include a sidebar.php, just include an empty sidebar.php, and leave the real one to the sidebar-ajax.php, for example.

To call the sidebar:

<?php get_template_part('sidebar'); ?>

And it should be something like this:

<div id="sidebar"></div>

In your functions.php:

add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
function theme_enqueue_scripts() {
    wp_enqueue_script('jquery');
    /* load your js file in footer */
    wp_enqueue_script('theme-script', get_stylesheet_directory_uri() . '/your-js-file.js', false, false, true);
}

add_action('wp_ajax_get_ajax_sidebar', 'check_ajax');
add_action('wp_ajax_nopriv_get_ajax_sidebar', 'check_ajax');
function check_ajax() {
    ?>
    get_template_part('sidebar-ajax');
    <?php
}

And in your-js-file.js:

jQuery.ajax({
    type: 'POST',
    url: location.href,
    data: { get_ajax_sidebar: 1 },
    success: function(data){
        jQuery('#sidebar').html(data);
    }
});
share|improve this answer
    
Sorry for belayted answer but your solution works fine. Thanks a lot. –  ptiswitz Oct 17 '12 at 9:48
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.