How to show the frontend always as logged-out

Sometimes you need to see the frontend as any other users who are logged out.

Of course, you can open the page in the Incognito window, and that’s it.

But in some cases, you should never see the frontend as a logged-in user, because if you do it the page is saved in the cache as it is, maybe with the admin topbar, or some editing links as shown in the following picture.

I don’t think you will always remember to use the incognito window and it may happen that the page is saved in the cache like it should not be saved.

This problem doesn’t occur with the page cache created by the popular caching plugins, but it’s very frequent with the CDNs if you also implement full page cache.

A typical issue with Cloudflare for example, if you implement the full page cache is that sometimes normal users see the editing inks on the page or even the admin top bar.

I think you should avoid this kind of serious issue.

 

You can easily be sure you never see the frontend as logged in user implementing the following simple code:

if(
 !is_admin()
 && !function_exists('wp_parse_auth_cookie')
 && !defined( 'DOING_AJAX' )
 && !defined( 'DOING_CRON' )
 && !isset( $_REQUEST['preview'] )
 && !isset( $_REQUEST['customize_changeset_uuid'] )
 && !isset( $_REQUEST['customize_theme'] )
 && !isset( $_REQUEST['post'] )
 && !isset( $_REQUEST['action'] )
 && !isset( $_REQUEST['_locale'] )
 && !isset( $_REQUEST['elementor-preview'] )
){
if( !function_exists( 'is_user_logged_in' ) ){
 function is_user_logged_in() {
  return false;
}

 function wp_parse_auth_cookie( $cookie = '',$scheme = '') {
  return false;
 }
 add_filter( 'show_admin_bar', '__return_false' );
 add_filter( 'get_edit_post_link','__return_false' );
}

In the code above we plug the functions “is_user_logged_in” and “wp_parse_auth_cookie” to return false every time the site is visited on the frontend. This will simulate a non-logged user.

We include also some conditions to prevent issues with the normal post preview and the customize and Elementor preview in case you use that plugin.

Now you can take advantage of the full page cache provided by the CDNs like Cloudflare without being worrying any more about this problem.

If you need to see the pages as logged-in, put the code inside a functional plugin and deactivate that plugin if in the URL you have for example ‘showme-all=true’. You can do it using Freesoul Deactivate Plugins by adding the match “showme-all=true” to the custom URLs in Freesoul Deactivate Plugins => Custom URLs => Frontend URLs.

If you have no functional plugin you can create it with WordPress Plugin Builder.

 

You can also download the plugin Frontend As Any User from the WordPress depository.

After activation, you will have an incognito icon in the admin top bar, to activate and deactivate the incognito mode.
Be careful, every administrator will be able to see the incognito mode according to his preferences, so you should instruct your collaborators and say to keep the incognito mode active if you want to avoid issues with the full page cache provided by the CDN.