0

Following my previous post (Full-Ajax Theme: parseJSON error while building a JSON object from a Wordpress custom template) regarding a few problems concerning JSON (now resolved), I am trying to load some alternative templates when an AJAX call is made.

Short version if you don't want to read the previous post:

On a clik on a specific .ajax-link, a request is sent through the header with

$.ajax({
            url    : url,
            type   : 'POST',
            headers: {
                'X-Requested-With':'MDXMLHttpRequest'
            }
        }).done(function() {
        //do stuff...
});

It is then calling the related function in my plugin :

add_filter( 'template_include', 'md_template_include' );
function md_template_include( $template ) {
    if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && $_SERVER['HTTP_X_REQUESTED_WITH']== 'MDXMLHttpRequest' ):
        $pre = dirname( $template );
        $suf = basename( $template );
        $_template = $pre . '/ajax-' . $suf;
        if( !file_exists( $_template ) )
            $_template = $template;
        $template = $_template;
    endif;
    return $template;
}

Which is adding the ajax prefix to the template name, and loading it instead of the regular one. This AJAX template is shrinked in content and wrote using JSON for data manipulation.

Since I am in a plugin folder, the solution to load the templates from the plugin folder was the following (that's the only solution I found, I am not sure if it is good practice... )

function get_single_cpt_template($single_template) {
    global $post;

    if ($post->post_type == 'project') {
        $single_template = dirname( __FILE__ ) . '/templates/single-project.php';
    }
    return $single_template;
}

Or again for an ajax specific single post template:

function get_ajax_single_cpt_template($ajax_single_template) {
    global $post;

    if ($post->post_type == 'project') {
        $ajax_single_template = dirname( __FILE__ ) . '/templates/ajax-single-project.php';
    }
    return $ajax_single_template;
}

All of that is working well!! without a problem, really smoothly, JS proof (if JS is disabled) and also SEO friendly... It allows all the website to still work without AJAX, and based on the History API, without the History API, AJAX is disabled and the navigation is the good ol' way.

The problem is that I can't find a solution concerning the index.php file. It doesn't want to load it using AJAX in any case... When I'm clicking on my website logo/title, it should load the ajax-index.phpbut it doesn't seem to work..

I did the exact same process as for the other templates... except the condition:

function get_ajax_index_template($ajax_index_template) {
        global $post;

        if (is_home_page()) {
            $ajax_index_template = dirname( __FILE__ ) . '/templates/ajax-index.php';
        }
        return $ajax_index_template;
    }

any idea ? I guess it is something I forgot about template hierarchy in Wordpress? Thank you for your time and your help.

UPDATE

It is loading the full index.php template: if I am looking at the network requests in Firebug, there is a "POST" but what is returned is index.php and not ajax-index.php... Making, therefore, the JS crash because the code is not the one it should be...

1 Answer 1

0

There is no is_home_page() function in Core. You have either is_home() or is_front_page() and you have to be careful about the two. They can be squirrelly.

I don't know which of the two functions you need but sorting that out should solve the problem, or use (is_home() || is_front_page()) perhaps.

4
  • Thank you for your answer. I read your other post and tried the (is_home() || is_front_page())condition, it is unfortunately not fixing the problem: the index.php template is still returned instead of ajax-index.php :/ Maybe it can help to precise that in my settings, the permalinks are on: %postname%, and the frontpage settings are set to display the latest posts.
    – JetXS
    Jun 1, 2015 at 14:21
  • Inside md_template_include(), put var_dump(get_queried_object(). What is that?
    – s_ha_dum
    Jun 1, 2015 at 14:35
  • Humm, if I placed it at the good line (before return $template ? Sorry it is my second wordpress theme there is some functions I don't know yet ahah! ) it is breaking kinda everything and saying NULL at the top of the main page (index.php) sorry, when clicking on a single post it is giving an array as a response looking like the post parameters, but if I click again on my blog title, it is printing, the server response is NULL
    – JetXS
    Jun 1, 2015 at 14:46
  • it is fixed when i move the file "ajax-index.php" next to index.php in the theme directory... the problem seems to come from that piece of code then: function get_ajax_index_template($ajax_index_template) { global $post; if (is_home() || is_front_page()) { $ajax_index_template = dirname( __FILE__ ) . '/templates/ajax-index.php'; } return $ajax_index_template; } When the ajax-index.phpfile is in the plugin folder... :/
    – JetXS
    Jun 1, 2015 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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