WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_head logs me out somewhat randomly (6 posts)

  1. Bonesnap
    Member
    Posted 1 year ago #

    I'm creating a website that has a front-end login and dashboard. I have created my login form and several "authentication required" pages. They worked flawlessly.

    However, one of my authentication-required pages would constantly log me out. I could go to the page, but once I went to it, if I tried to access another page that required authentication, it would log me out and I'd be forced to log in again.

    I have no idea what was causing it; I used the same code that I was using for the other pages. Eventually I deleted the custom template and header files as well as the page in the WP dashboard. I figured I'd start from scratch on that page since I was obviously missing something that my eyes weren't catching.

    As soon as I did that a different page was exhibiting the same behaviour. I started commenting out code to see what could possibly be causing this and I've narrowed it down to the wp_head() function that's called in the header. If I comment it out, I can stay logged in and go about my merry way. If I leave it in, I get logged out every time I visit THAT page.

    I know I can't remove it because it's one of the most important hooks, but since I have narrowed it down to that function, I did a print_r() on the $wp_actions variable to see if there was any difference and there isn't. This is the output:

    Array
    (
        [muplugins_loaded] => 1
        [registered_taxonomy] => 10
        [registered_post_type] => 11
        [plugins_loaded] => 1
        [sanitize_comment_cookies] => 1
        [setup_theme] => 1
        [load_textdomain] => 2
        [after_setup_theme] => 1
        [auth_cookie_malformed] => 1
        [auth_cookie_valid] => 1
        [set_current_user] => 1
        [init] => 1
        [widgets_init] => 1
        [register_sidebar] => 5
        [wp_register_sidebar_widget] => 14
        [wp_loaded] => 1
        [parse_request] => 1
        [send_headers] => 1
        [parse_query] => 1
        [pre_get_posts] => 1
        [posts_selection] => 1
        [wp] => 1
        [template_redirect] => 1
        [get_header] => 1
        [wp_default_scripts] => 1
    )

    I have added back the original page and used the same code as before, and it is working as expected. Truly perplexing.

    Some more information:

    - I'm using the wp_signon() function to do the logging in.
    - I'm using the is_user_logged_in() function to check if the user is logged in long before the wp_head() function is called.
    - If I view a page that does not require authentications, such as the home page, it works as expected: that is, I'm still logged in. But, as soon as I visit THAT page I get logged out.

    Is this a bug, or am I misunderstanding something about WordPress?

    Any help would be greatly appreciated. If you need code for anything, just ask.

    Thank you for your time.

  2. camper2020
    Member
    Posted 1 year ago #

    Hey man, I was having a remarkably similar issue to you, which has taken me about a week to resolve, not sure if you ever came right?

    Anyway, I think I have the solution, WordPress adds post relation links to the wp_head action. These are <link /> tags which actually call a page, the same way a CSS file is called. Anyway, I'm assuming in addition to your custom sign on page, you have a custom login page, which you have defined. If you look at your source of your pages, and in particular the page that keeps signing you out, one of these relative pages is probably the logout page. Mine was. By deleting that page, the rel then moved to another page which is why a page that was working before, stopped working.

    The solution is to remove this particular action from wp_head, its not really all that important to keep anyway.

    Add the following to your functions.php file of theme or your custom plugin:

    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

    Let me know if that helps, if you already came right, then good show.

    Regards.

  3. Bonesnap
    Member
    Posted 1 year ago #

    I actually did realize this was the cause sometime last week. I only figured it out though because it only happens in Firefox, and I had run into an issue in the past that only affected Firefox and this was the solution, so I decided to try it out and it worked.

    I now use it in all my WordPress websites as I don't see any benefit of having it (feel free to correct me). Good find though!

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Never remove the call to wp_head() in a theme. It's used internally y WordPress and by many plugins.

  5. Bonesnap
    Member
    Posted 1 year ago #

    I am well aware of that and even say it in my original post.

  6. camper2020
    Member
    Posted 1 year ago #

    Hi Bonesnap, great, glad you came right. Its interesting that it only affects Firefox, good spot!

    I was nervous that this action might have something to do with the behaviour of functions like previous_posts_link() but it appears that there is no dependency and as yet, can't see a proper use for that particular action, unless there is an SEO reason that I'm missing. So removing it definitely seems to be the best solution without side affects (that I'm aware of).

    @esmi, always good to be reminded on that, but as Bonesnap mentioned, we by no means suggest the removal of the wp_head hook, only this one particular action from the hook.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.