How to Remove Recent Comments CSS from WordPress Header (wp_head)

Home » Snippets » How to Remove Recent Comments CSS from WordPress Header (wp_head)
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re a WordPress developer or power user, you may be familiar with the wp_head function in WordPress. This function adds various elements to the head section of your site, such as the site’s title, scripts, and stylesheets. However, if you’ve ever tried to remove the default CSS for recent comments that gets added to wp_head, you may have noticed that it’s not as straightforward as it seems. In this article, we’ll show you how to remove the recent comments CSS from wp_head, so your site loads a little faster and looks exactly how you want it to.

					function wpturbo_remove_recent_comments_style() {
    global $wp_widget_factory;

    remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
}

add_action( 'widgets_init', 'wpturbo_remove_recent_comments_style' );
				

The code above presents a solution to a common problem in web development with WordPress: removing recent comments WP_HEAD CSS. By default, WordPress will add some CSS to the head of your website which will display the recent comments widget in the sidebar.

This is not always the best option for website owners who want to customize their website’s look and feel.

The first step in this code is to define the wpturbo_remove_recent_comments_style() function. The function contains two actions: global and remove.

The global action is used to make the $wp_widget_factory variable accessible inside our wpturbo_remove_recent_comments_style() function. This variable is necessary for the remove_action() function to work properly and be able to locate the recent comments widget object.

The remove_action() function is what actually removes the recent comments widget CSS from the WP_HEAD. We get the widget object from the $wp_widget_factory array and use the recent_comments_style method to specify the style that we want to remove.

Finally, we add an add_action() function that hooks wpturbo_remove_recent_comments_style() into the widgets_init action.

This action ensures that the custom function is triggered when WordPress initializes the widgets. Hence, the recent comments widget CSS that was previously displayed on the front-end and not hidden is now removed.

Overall, this code provides a quick and simple solution for developers who want to improve their website’s customizability and remove recent comments WP_HEAD CSS.

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