• Resolved Whithil XBR

    (@whithil-xbr)


    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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Whithil XBR

    (@whithil-xbr)

    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)
    {
      get_header();
    }
    else
    {
      $_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.

    hey I just went looking for the same thing and found this reference in – https://codex.www.ads-software.com/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.

    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: https://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: https://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).

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to check if the header is already loaded?’ is closed to new replies.