Remove the default css styles tag in wp_head

2k Views Asked by At

I would like to remove the default css styles tag in wp_head, such that:

BEFORE :

<head>
<link/>
<script/>
<style> 
...
...
...
</style>
</head>

AFTER :

<head>
<link/>
<script/>
</head>

How can I accomplish this?

2

There are 2 best solutions below

0
On

If you are talking about inline css, you need to investigate through your theme and your different plugin.

Basically, any style is injected with the action wp_head.

For example

add_action('wp_head', 'custom_function');

To remove these entries, if they are related to the style, you need to use remove_action()

remove_action('wp_head', 'custom_function');

This way will work fine with procedural, if the action is thrown within a class

remove_action('wp_head', array('class_name', 'custom_function');

Don't forget to use the same priority for the remove_action() than the add_action(), if need. You can, also, remove an action, and add your own custom function with your own style definitions with a new add_action calling your function and move it where you want in the head when playing with the priority.

0
On

Search the wp_enqueue_style method and show, which methods calling deeper.

wp_enqueue_style > wp_styles > $wp_styles

After you have tracked the methods, you see, you must handle the variable $wp_styles.

Create an init-Action in your Theme and override $wp_styles:

function removeStyles() {
    global $wp_styles;

    $wp_styles = null;
}

That's all. Another way ist, to get all registred style-names to deregister it.