I used a snippet to create WooCommerce login / logout links via functions.php, present on literally every single Wordpress-related website and highly upvoted. As usual it doesn't really work.

Once logged in, moving to pages visited previously as anonymous user, causes their cached version to be displayed. It means that "Log in" link will appear, despite the fact that user is already logged. Refreshing page will correct menu link, displaying "Log out", but it can't be a solution from user point of view.

I use plugins WP Fastest Cache, Autoptimize and my server is connected to Cloudflare CDN. On top of it there is browser cache.

Rather than getting rid of any solution mentioned above, as they give me excellent website speed and love from Google (98 mobile PageSpeed score on product page) and difficult workarounds, if possible, I would like to make menu cache immune.

How can I achieve this? Any jQuery or Ajax solution? I guess I'll need to find whether wordpress_logged_in cookie is set like in this topic:


I will figure out the code, just need a direction to avoid dead ends.

  • 3
    Please read the wordpress tag you included. You are asking an off-topic question. See On Topic. Questions about wordpress.com belong on Web Applications. Questions about installing and maintaining WordPress belong on WordPress Development Commented Apr 16, 2019 at 19:53

1 Answer 1


Solution from:


was a dead end. It does not work with WooCommerce, because login is not done through standard WordPress login screen, but dedicated login form and there are no cookies accessible through JavaScript created when user logs in.

I created a working solution through jQuery Ajax, though I'm not happy with result, because links appear with some delay. At least they show up correctly for cached pages and user is not confused.

Then I sought for something both reliable and fast, what I perfectly found in this answer by Janos Szabo which shows how to create and delete script-accessible cookies on login and logout:

Check if user is logged in using JQuery

For anyone interested here is the code of my Ajax solution:

Contents of file "taisho.js" in "js" folder of child theme:

    type: "post",
    url: my_ajax_object.ajax_url,
    data: {
        action: 'is_user_logged_in'
    success: function(response){        
        var menu_links = response == 'yes' ? logged : anonymous;
        $('nav.secondary-navigation').html('<ul id="menu-topmenu" class="menu">' + menu_links + '</ul>');       

Contents of my functions.php file:

// Store login and my account + logout links as js variables for conditional display, to be used in js file.
add_action('wp_footer', 'taisho_login_logout');
function taisho_login_logout() {
    <script type="text/javascript">

        var anonymous = <?php echo json_encode(
            '<li><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">Log in</a></li>'
        var logged = <?php echo json_encode(
            '<li><a href="'. get_permalink( woocommerce_get_page_id( 'myaccount' )) . '">My account</a></li>
            <li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) . '">Log out</a>               </li>'


// Enqueue js file residing in current (parent or child) theme and enable Ajax url in this file through wp_localize_script.
add_action( 'wp_enqueue_scripts', 'taisho_login_check' );
function taisho_login_check() {
    wp_enqueue_script( 'taisho-login-check', get_stylesheet_directory_uri() . '/js/taisho.js', array('jquery'));
    wp_localize_script( 'taisho-login-check', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

// Check if user is logged in through Ajax.
add_action('wp_ajax_is_user_logged_in', 'ajax_check_user_logged_in');
add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_user_logged_in');
function ajax_check_user_logged_in() {
    echo is_user_logged_in()?'yes':'no';

Your Answer

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

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