[resolved] How to check if the header is already loaded? (4 posts)

  1. Whithil XBR
    Posted 2 years ago #

    my Ajax plugin loads the header twice sometimes and it breaks the page in my theme. but removing the "<?php get_header(); ?>" for instance, in single.php solves the loading problem, but the page would not be loaded correctly if it was opened directly in the browser.

    How can I insert a code, instead of "<?php get_header(); ?>" so it loads the header only when it's not done yet?

  2. Whithil XBR
    Posted 2 years ago #

    After googling a bit the problem is now Solved!
    thanks to Bell

    If the page was refreshed then you'd expect two requests following each other to be for the same URL (path, filename, query string), and the same form content (if any) (POST data). This could be quite a lot of data, so it may be best to hash it. So ...

    And here is a snippet based on his original example to use instead of <? get_header() ?>

    <?php session_start();
    //The second parameter on print_r returns the result to a variable rather than displaying it
    $RequestSignature = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].print_r($_POST, true));
    if ($_SESSION['LastRequest'] == $RequestSignature)
      $_SESSION['LastRequest'] = $RequestSignature;
    } ?>

    In an AJAX situation you'd have to be careful about which files you put this code into so as not to update the LastRequest signature for scripts which were called asynchronously.

  3. dains
    Posted 2 years ago #

    hey I just went looking for the same thing and found this reference in - http://codex.wordpress.org/Function_Reference/load_template
    $wp_did_header Returns true if the WordPress header was already loaded. See the /wp-blog-header.php file for details.

    so it looks like you could use this as a conditional to determine whether to load the header or not.

  4. jorgeorpinel
    Posted 1 year ago #

    You may want to re-factor your use of ajax to do it "the right way" (there seems to be no consensus really) e.g. as described here: http://natko.com/wordpress-ajax-login-without-a-plugin-the-right-way/

    Your method works of course and I've implemented similar hacks before but it can get really messy and make your code impossible to maintain (if you care about that).

    This thread may also be of your interest: http://wordpress.stackexchange.com/questions/69184/how-to-load-wordpress-on-non-wp-page (I've been researching about similar issues recently so I have those references).


Topic Closed

This topic has been closed to new replies.

About this Topic