How to Hide the Toolbar When There’s an Error in WordPress

Home » Snippets » How to Hide the Toolbar When There’s an Error in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever encountered errors on your WordPress website that caused the toolbar to display unnecessary information? It can be frustrating and distracting, especially when you’re trying to troubleshoot and fix the issue at hand. In this article, we will explore how you can hide the toolbar when there’s an error on your WordPress site, allowing you to focus on resolving the problem without any distractions. Let’s dive in and learn how to streamline your troubleshooting process.

					function wpturbo_hide_toolbar_on_error() {
    if (is_admin()) {
        $current_screen = get_current_screen();
        
        if (is_object($current_screen) && isset($current_screen->parent_base) && $current_screen->parent_base == 'edit' && isset($_GET['error'])) {
            show_admin_bar(false);
        }
    }
}
add_action('admin_init', 'wpturbo_hide_toolbar_on_error');
				

The given code snippet is used to hide the toolbar in WordPress admin when there is an error. Let’s break down the code to understand how it works.

The wpturbo_hide_toolbar_on_error() function is defined to handle the logic for hiding the toolbar. This function is hooked into the admin_init action, which is fired when the admin area is initialized.

The first line inside the function checks if the current page is the admin area by using the is_admin() function. This ensures that the code only runs in the admin area and not on the front end of the website.

Next, we retrieve the current screen object using the get_current_screen() function. This function returns an object that represents the current admin screen. We store this object in the $current_screen variable for later use.

The following if statement checks if the $current_screen variable is an object and also checks if the parent_base property of the $current_screen object is set to 'edit'. This condition is used to target the edit screens in the admin area, where the toolbar is usually displayed. Additionally, we check if the $_GET['error'] variable is set. This checks if there is an error parameter in the current URL.

If all of these conditions are met, we execute the show_admin_bar(false) function. This function is used to hide the admin toolbar when the parameter $_GET['error'] is set.

To summarize, this code snippet checks if the current page is in the admin area, if it is an edit screen, and if there is an error parameter in the URL. If these conditions are met, the admin toolbar is hidden using the show_admin_bar(false) function.

By using this code snippet, you can create a better user experience by hiding the toolbar when an error occurs in the admin area, allowing the user to focus on resolving the error without any distractions.

Register an account to save your snippets or go Pro to get more features.