WordPress View Frontend as a Guest

This is particularly useful when using cache like Cloudflare, and don’t want the editor’s frontend page view to be cached.

//admin or editor temporarily logout when on frontend
function front_guest_view() {
	global $current_user;
	//if its admin panel, don't logout
	if(is_admin() or is_customize_preview() ) 
		return;
	//if user can edit post, show a guest frontend page
	if( current_user_can( 'edit_posts' ))
		$current_user = -1 ;
}
add_action( 'template_redirect', 'front_guest_view', 20 );

Leave A Comment

CAPTCHA ImageChange Image