6

I followed the common advise to use wp_head() in header.php and wp_enqueue_scripts() instead of hard linking css and js files in header.php.

However, I realized that wp_head() outputs lots of stuff I'm not sure if I really want it to be there. E.g. there is some CSS stylesheet stuff dealing with emotiis and smileys ... for whatever reason.

//UPDATE

Despite my writing above, I am pretty aware of what parts of wp_head() I want to have in my HTML Head section.

//UPDATE II

I am developing my own theme and I have only a single plugin in use which is Pods.io.

I did some research but found solutions only that suggest to subsequently alter wp_head() output via output buffering methods (see Remove an action from an external Class). To me, that appears to be a rather dirty solution.

Hence, my question is, how can I exactly define the default output of wp_head()? What hooks are available to trigger certain output parts? Pls note, I am aware of how to add content to wp_head() via hooks but not how to precisely remove unwanted output.

4
  • 2
    Here is everything hooked to wp_head. Also, check this on how to remove emojicons Jan 13, 2016 at 11:34
  • @PieterGoosen: Cool, so with that information I can simply remove certain default actions, am I right? E.g. remove_action('wp_head', 'wp_print_styles');
    – Bunjip
    Jan 13, 2016 at 12:17
  • That is correct. Just make sure, actions needs to be removed with the same priority which was used when they were added Jan 13, 2016 at 12:24
  • Fine. Allright, got it. If you'd reply this as an answer, I could mark the question solved by your answer
    – Bunjip
    Jan 13, 2016 at 12:27

3 Answers 3

13

Here is the current list of actions that is currently hooked by default to wp_head

Reposted here to avoid unnecessary opening multiple browser windows

add_action( 'wp_head',             '_wp_render_title_tag',            1     );
add_action( 'wp_head',             'wp_enqueue_scripts',              1     );
add_action( 'wp_head',             'feed_links',                      2     );
add_action( 'wp_head',             'feed_links_extra',                3     );
add_action( 'wp_head',             'rsd_link'                               );
add_action( 'wp_head',             'wlwmanifest_link'                       );
add_action( 'wp_head',             'adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head',             'locale_stylesheet'                      );
add_action( 'wp_head',             'noindex',                          1    );
add_action( 'wp_head',             'print_emoji_detection_script',     7    );
add_action( 'wp_head',             'wp_print_styles',                  8    );
add_action( 'wp_head',             'wp_print_head_scripts',            9    );
add_action( 'wp_head',             'wp_generator'                           );
add_action( 'wp_head',             'rel_canonical'                          );
add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
add_action( 'wp_head',             'wp_site_icon',                    99    );

if ( isset( $_GET['replytocom'] ) )
    add_action( 'wp_head', 'wp_no_robots' );

You can remove any action with remove_action()

remove_action( 'wp_head', 'wp_print_styles, 8 );

Just remember, an action need to be removed with the same priority it was added.

As bonus, check this epic post from @ChristineCooper on how to remove emojicons

EDIT

An important note here. The foillowing actions should not be removed as this causes serious issues with how stylesheets and scripts are loaded

  • locate_stylesheet

  • wp_print_styles

  • wp_generator

  • wp_enqueue_scripts

If you need something specific removed, rather remove the call back function with remove_action or use the specific functions allocated to remove styles and scripts

0
0

You are not suppose to remove default WordPress styles (which are included from wp-includes, they are all there for some reasons).

You can remove styles which are included from theme by creating child theme.

10
  • 4
    You are not suppose to remove default WordPress styles) WHY NOT??? That is the purpose of actions and filters in the first place, to remove/modify/add to core features as you see fit Jan 13, 2016 at 11:54
  • because they are there for some reason... i.e. admin-bar.min.css is for displaying admin header if user is logged in. I mean you should not remove default styles and js, If you want to do so then go for core PHP instead Jan 13, 2016 at 11:57
  • And if don't like the way admin header is displayed...? Jan 13, 2016 at 12:01
  • There are functions for that too...try <?php show_admin_bar( $bool ); ?> removing css won't remove header. Jan 13, 2016 at 12:03
  • @MayurChauhan: I am very aware of what parts of the default WP core wp_head() output I'd like to remove. Maybe, my original writing was a bit sloppy at this part. I am looking for a proper way to remove parts I really don't want to be there.
    – Bunjip
    Jan 13, 2016 at 12:10
0

The wp_head hook is simply a hook.

Themes and plugins use this hook to add stuff they need in the header, like css and js. Very often they enqueue unnecessary scripts in every page instead of limiting them in specific pages.

So you can either check your plugins and theme where it enques the scripts and edit them or use the remove_action to remove them.

2
  • 1
    In some rare occasions perhaps Not true and not supported by any facts. More than often plugin and theme authors hook a bunch of unnecessary crap to wp_head. If you look at a few questions here, even commercial themes and plugins AND core have unnecessary stuff clotting up wp_head. Would be nice to know where you get your info from Jan 13, 2016 at 11:51
  • You are right, I changed it to "very often".
    – panos
    Jan 13, 2016 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.