Hot answers tagged

27 votes

Add <meta> tags to the <head> section via functions.php

The hook you're looking for is specifically wp_head which could look something like this: function theme_xyz_header_metadata() { // Post object if needed // global $post; // Page ...
  • 19.8k
12 votes
Accepted

How to control initial wp_head() output?

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', '...
10 votes
Accepted

Two title tags in my header

The two title tags can be explained as that you are using a theme that is written for Wordpress4.1 and actually is using 4.1. As from 4.1 you don't need to call wp_title() in the head any more, you ...
10 votes
Accepted

wp_head() gives me some weird CSS

The fact that <style type="text/css" media="screen"> html { margin-top: 32px !important; } * html body { margin-top: 32px !important; } @media screen and ( max-width: 782px ) { ...
  • 726
10 votes
Accepted

How can i remove JUST the title tag from wp_head() function?

You can see everything added to wp_head in the file /wp-includes/default-filters.php. If your theme supports the title tag, you can remove it entirely with remove_action: remove_action( 'wp_head', '...
  • 76.6k
8 votes

Remove meta robots tag from wp_head

add_filter('wpseo_robots', 'yoast_no_home_noindex', 999); function yoast_no_home_noindex($string= "") { if (is_home() || is_front_page()) { $string= "index,follow"; } return $...
8 votes
Accepted

Hook into wp_head(); in a plugin

Did you try this? function hook_header() { $example_position = get_theme_mod( 'logo_placement' ); if( $example_position != '' ) { switch ( $example_position ) { case 'left':...
  • 4,908
8 votes
Accepted

How can i get the name parameter defined in get_header?

There is an action get_header that you can use. In your theme’s functions.php, register a callback for that action: add_action( 'get_header', function( $name ) { add_filter( 'current_header', ...
  • 105k
8 votes
Accepted

Is there a way to read or list wp_head() contents?

I wanted to search-and-replace in the header, but Neither @majick or @Samuel Elh answers worked for me directly. So, combining their answers I got what eventually works: function start_wp_head_buffer(...
  • 198
6 votes
Accepted

Caching and Versioning for rtl.css

You are loading the rtl.css using the WordPress automatic way, that is, having a rtl.css in the theme folder will be loaded by WordPress automatically if it presents and direction of current language ...
  • 19.9k
6 votes
Accepted

Remove rel='dns-prefetch' href='//maps.google.com' from wp-head

Resource Hints is a smart feature added to WordPress version 4.6. I think it might improve your site speed. But if you want to disable it, try this: remove_action('wp_head', 'wp_resource_hints', 2); ...
  • 3,389
5 votes

wp_head() not inserting the default stylesheet style.css

Actually you shouldn't add JS and CSS files to your header.php, but make use of the functions wp_enqueue_script() and wp_enqueue_style() to add them there. Example taken from the codex page: /** * ...
5 votes
Accepted

Any advantage of using wp_scripts and is_IE when enqueuing scripts

To extend on @gmazzap suggestion on not using globals when you can use wp_scripts(), there is a shortcut for wp_scripts() for adding conditional comments called wp_script_add_data and likewise ...
  • 4,239
5 votes

Remove specific CSS and JS from the head

When you look at the <style|script> tags in your head, you will notice the id attribute there. Example: id='bootstrap-css' This – in most cases – was added by a function hooked to ...
  • 50k
5 votes
Accepted

Custom script file enqueue has "?ver=4.5.1" when loading and doesn't update

I agree with @Mark Kaplun - what you're describing is an odd behavior though. Try this markup to override the $ver param. wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', ...
5 votes
Accepted

wp_head() outputs in body

This is your browser's attempt to avoid invalid markup. Your echo ' test'; outputs something that does not belong in a head tag, so the browser closes it and opens the body tag. If you look at the ...
  • 76.6k
5 votes
Accepted

Removing specific style from wp_head

Note: the file name is functions.php, not function.php (but that's probably just a typo in the question). To remove a script or style, you must remove it after it was added. If you try to remove it ...
  • 351
4 votes

How to add stylesheets only to pages with specific shortcode?

This works for me: function register_my_css () { wp_register_style('my-style', plugins_url('styles.css', __FILE__)); } function register_my_scripts () { wp_register_script('my-script', ...
4 votes
Accepted

How to add iOS & fav icons to the theme?

Hook into wp_head in your functions.php file. add_action('wp_head', 'add_your_stuff'); function add_your_stuff() { ?> <link rel="shortcut icon" href="<?php echo ...
  • 3,433
4 votes

How to remove pingback from head?

Here's my improvement on it with less code for the same results: add_action( 'plugins_loaded', 'wpse_158700_buffer' ); function wpse_158700_buffer() { # Enable output buffering ob_start( '...
4 votes

Disable wordpress from including jQuery in the head section

Replacing the WordPress version of jQuery is generally a bad idea. Checkout Pippin's article Why Loading Your Own jQuery is Irresponsible for more information. Instead of completely replacing jQuery,...
  • 549
4 votes
Accepted

Change title in head on Archive page

You can change it via the category template (if you have multiple category templates you'll need to edit all of them; if you have archive.php, you need to edit it only there). Otherwise you can use ...
4 votes
Accepted

ACF custom field in <head>

In the header.php template, you cannot access the $post variable. So you will have to map it using $wp_query, as mentioned on ACF forum here. Try this: <?php global $wp_query; $post = $...
  • 156
4 votes

Any advantage of using wp_scripts and is_IE when enqueuing scripts

And option 5 is to detect on client side what browser it is, and create a script element for your script directly into the dom, like google analytics an FB SDK do. This has the advantage of doing ...
  • 22.9k
4 votes

Any advantage of using wp_scripts and is_IE when enqueuing scripts

Generally speaking, scripts and styles should never be directly printed to page. Reason is that another plugin or theme might add the same script again, and you get the same script added 2 or more ...
  • 44.9k
4 votes
Accepted

Putting content into header.php without using wp_head

Removing wp_head will have some negative effects as it won't allow you to use virtually 90% of the plugins on wordpress.org. Also some of that code you are seeing only shows up when you are logged in (...
  • 4,239
4 votes
Accepted

How to get a single hook from wp_head()?

wp_head is an essential part of the theme, and there is too many things that might fall apart if it is not there. Hunting and fixing them one by one might be time consuming and the worthiness of the ...
  • 22.9k
4 votes

How to hook into <head></head> container

to hook in the <head> you would use wp_head. You can read the documentation here. add_action( 'wp_head', 'meta_example' ); function meta_example() { echo '<meta name="description" ...
  • 316
4 votes
Accepted

Change Page's <title> Tag Using functions.php File

Check this out function custom_title($title_parts) { $title_parts['title'] = "Page Title"; return $title_parts; } add_filter( 'document_title_parts', 'custom_title' ); In custom_title, $...
4 votes
Accepted

Display custom_background outside wp_head()

custom_background() places the CSS to wp_head() as you mentioned so to get those CSS as if you don't have wp_head() in your header, these tweaks from the core files would help: function ...
  • 1,888

Only top scored, non community-wiki answers of a minimum length are eligible