How to Remove the Comments Link from the WordPress Admin Bar

Home » Snippets » How to Remove the Comments Link from the WordPress Admin Bar
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress comes with a default feature that displays a comments link in the admin bar on both the front-end and back-end of your site. While this may be useful for some users, if you do not want to show this link, you may be wondering how to remove it. In this article, we will be showing you how to remove the comments link from the admin bar using simple code snippets. So, without further ado, let’s get started!

					function wpturbo_remove_comments_link() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu( 'comments' );
}
add_action( 'wp_before_admin_bar_render', 'wpturbo_remove_comments_link', 999 );
				

The code snippet above removes the comments link from the WordPress admin bar. This can be useful if you want to customize the admin bar to show only the links that are relevant to your site or to your clients’ needs.

The first step in the code is to define a function named wpturbo_remove_comments_link(). This function will be responsible for removing the comments link on the admin bar.

Next, we add a global variable $wp_admin_bar. This variable refers to the main instance of the WordPress admin bar. By adding this variable, we can manipulate the admin bar and remove the comments link.

The next line of code calls the remove_menu() method on the $wp_admin_bar variable. The remove_menu() method takes one parameter, which is the ID of the menu that we want to remove. In this case, we’re removing the ‘comments’ menu, which is the default WordPress comments link on the admin bar.

Finally, we hook the wpturbo_remove_comments_link() function into the wp_before_admin_bar_render action. This action fires just before the admin bar is rendered on the page. By adding a high priority of 999, we ensure that our function runs last, after other functions that may be manipulating the admin bar. This ensures that our function will successfully remove the comments link from the admin bar.

Overall, this code snippet is a simple but effective solution for removing the comments link from the WordPress admin bar. With a few tweaks, it can be adapted to remove other links as well, depending on your needs and preferences.

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