• Hi there,

    We’re trying to achieve page caching for logged-in users, but this comes with some unwanted side effects.

    The main problem is that we’re seeing the admin-bar appear for visitors who are not logged in. So it seems W3TC is serving the same cached file to multiple user groups. Of course this shouldn’t happen.

    We really need caching for logged-in users and the admin-bar to appear, so disabling one or both of them isn’t an option. How can we achieve our goal?

    We’ve added some Ajax calls for loading user-specific data, but we’re not sure if this can be done for the admin bar.

Viewing 1 replies (of 1 total)
  • Plugin Contributor gidomanders

    (@gidomanders)

    I’m sorry for the late reply. I achieved the same using these pieces of code:

    functions.js

    
    
    add_filter('show_admin_bar', 'only_show_admin_bar_in_ajax_request', 99);
    function only_show_admin_bar_in_ajax_request() {
        return is_user_logged_in() && stripos($_SERVER['REQUEST_URI'], 'ajax_display_admin_bar') !== false;
    }
    
    add_action('wp_ajax_ajax_display_admin_bar', 'ajax_display_admin_bar');
    add_action('wp_ajax_nopriv_ajax_display_admin_bar', 'ajax_display_admin_bar');
    function ajax_display_admin_bar() {
        if (only_show_admin_bar_in_ajax_request()) {
            /** @var WP_Admin_Bar $wp_admin_bar */
            global $wp_admin_bar;
    
            /* Load the admin bar class code ready for instantiation */
            require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
    
            /* Instantiate the admin bar */
    
            /**
             * Filters the admin bar class to instantiate.
             *
             * @since 3.1.0
             *
             * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
             */
            $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
            if ( class_exists( $admin_bar_class ) ) {
                $wp_admin_bar = new $admin_bar_class;
            } else {
                exit;
            }
    
            $wp_admin_bar->initialize();
            $wp_admin_bar->add_menus();
    
            echo '<link rel="stylesheet" id="admin-bar-css" href="//www.steenks-service.nl/wp-includes/css/admin-bar.min.css" type="text/css" media="all">';
            echo '<link rel="stylesheet" id="dashicons-css" href="//www.steenks-service.nl/wp-includes/css/dashicons.min.css" type="text/css" media="all">';
            echo '<link rel="stylesheet" id="bottom-admin-bar-css" href="' . plugins_url( 'bottom-admin-bar/css/view.css' ) . '" type="text/css" media="all">';
    
            /**
             * Load all necessary admin bar items.
             *
             * This is the hook used to add, remove, or manipulate admin bar items.
             *
             * @since 3.1.0
             *
             * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
             */
            do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
    
            /**
             * Fires before the admin bar is rendered.
             *
             * @since 3.1.0
             */
            do_action( 'wp_before_admin_bar_render' );
    
            $wp_admin_bar->render();
    
            /**
             * Fires after the admin bar is rendered.
             *
             * @since 3.1.0
             */
            do_action( 'wp_after_admin_bar_render' );
        }
        exit;
    }
    

    main.js

    
    $(function () {
    $.ajax({
            method: 'GET',
            url: window.location.href,
            async: true,
            data: {
                action: 'ajax_display_admin_bar'
            },
            success: function (response) {
                $('body').append(response);
            }
        });
    });
    
Viewing 1 replies (of 1 total)
  • The topic ‘Admin bar shows when not logged in’ is closed to new replies.