Hot answers tagged

19

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 conditional if needed // if( is_page() ){} ?> <meta name="abc" content="xyz" /> <?php } add_action( 'wp_head', 'theme_xyz_header_metadata' ...


11

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', ...


10

Loading Scripts and Styles Dynamically Using Shortcode Advantages Does not search through all the posts every time the shortcode is called. Able to add styles as well as scripts dynamically only when shortcode is on the page. Does not use regexes since they tend to be slower than strstr() or strpos(). You could switch to regex if you need to accept args. ...


10

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 can make use of new title_tag theme support tag which automatically adds the wp_title() tag in the header The parent theme you are using are most probably ...


10

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 ) { html { margin-top: 46px !important; } * html body { margin-top: 46px !important; } } </style> is added for Admin bar by wp-core at the ...


9

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', '_wp_render_title_tag', 1 ); Though it may be simpler/better to use remove_theme_support( 'title-tag' ) in a child theme, which is what _wp_render_title_tag ...


8

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 $string; } this should be fine i think.. somewhere in your theme functions.php and should do the trick.


8

Pull the add_action() outside of the function, and put the conditional inside the callback. Also, if you're printing a script directly, use wp_print_scripts instead of wp_head. You also have a syntax error. function testingone(){ if( get_option( 'MyOptionName' ) ) { ?> <script>var Script = GoesHere; </script> <?...


8

Did you try this? function hook_header() { $example_position = get_theme_mod( 'logo_placement' ); if( $example_position != '' ) { switch ( $example_position ) { case 'left': // Do nothing. The theme already aligns the logo to the left break; case 'right': echo '<style ...


8

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', function() use ( $name ) { // always return the same type, unlike WP return (string) $name; }); }); You could also write a little helper class ...


8

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() { ob_start(); } add_action('wp_head','start_wp_head_buffer',0); function end_wp_head_buffer() { $in = ob_get_clean(); // here do whatever you ...


6

You problem lies with how you are using wp_enqueue_script(). It does not print out the <script src=""></script>, instead it adds it to a list of Javascript files to printed out at the appropriate time. There is a different hook that is called for when you should add your Javascript and CSS: wp_enqueue_scripts (you use it for CSS too). Your ...


6

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 ir rtl (you should add this information to the question, it has been difficult to figure it out how you are loading the file). This process defines ...


5

As you said per page/post basis, this would work for each post add_action('wp_head', 'add_link_in_head'); function add_link_in_head() { global $post; if(!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if(!empty($alternate) && !...


5

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: /** * Proper way to enqueue scripts and styles */ function theme_name_scripts() { wp_enqueue_style( 'style-name', get_stylesheet_uri() ); wp_enqueue_script( ...


5

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 wp_style_add_data for conditional styles. So the correct way to use conditionals as of Wordpress 4.2 is like this: /** * IE enqueue HTML5shiv with conditionals * @...


5

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); References wp_head resource hints wp_resource_hints filter


5

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 actual unprocessed response from the server, it won't appear this way, but that echo shouldn't be there regardless, and this issue will disappear when you remove ...


5

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 before it was added, or even print $GLOBALS['wp_scripts']->registered, nothing will happen, since it was not added yet. So a way to remove them is to execute ...


4

Use the below code in the <head> section: <?php add_action('wp_head', 'your_function'); function your_function() { //your code will go here } ?> For more information you can visit here.


4

You can try this (untested): add_action( 'wp_head', function(){ // your conditions: if( is_home() || is_category() ) { // access the global SyntaxHighlighter object instantiated at 'init'. global $SyntaxHighlighter; // remove your action hooks: remove_action( 'wp_head', ...


4

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 get_stylesheet_directory_uri();?>/favicon.ico" type="image/x-icon" /> <link rel="apple-touch-icon" href="<?php echo get_stylesheet_directory_uri();?>/apple-touch-...


4

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, you could instead move the supplied version to the footer. However you need to be mindful that this will most likely break plugins if they load scripts in the ...


4

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 the Yoast SEO plugin which have settings for naming the archives (actually I think that almost all SEO plugins have those settings).


4

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 = $wp_query->post; $background = get_field('background', $post->ID); ?> <style> .site-header { background: url(<?php echo $background; ?&...


4

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 browser detection in the only place it should be done, the browser, and loading the IE specific scripts only when needed. In spirit it is very similar to your ...


4

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 times. If you enqueue asset the proper way, if other code enqueue the same asset again, it will be added once. For this reason you should skip options 3 and 4. ...


4

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 wp_enqueue_scripts, wp_enqueue_styles or wp_register_scripts or wp_register_styles hook. In the worst case it was hooked to either the wp_head or the wp_print_scripts hook. ...


4

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 all operation questionable. In you snippet for example you want to remove things that make no sense to remove specifically only for one CPT, and if you have ...


4

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', array ( 'jquery' ), null, true); https://developer.wordpress.org/themes/basics/including-css-javascript/ Alternatively, to work around the version cache try ...


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