30
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 ...
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', '...
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(...
6
votes
Remove all theme CSS & JS from wp_head (but only for 1 page template)
Thank you for your help! This solution works with my theme:
function remove_all_theme_styles() {
if ( is_page_template('template-landing.php') ) {
global $wp_styles;
$wp_styles->queue = ...
6
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 ...
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 ...
4
votes
How to load different css file for different pages
You can achieve this using conditionals inside the function enqueuing your styles.
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'global', get_stylesheet_uri() );
if ( is_page(5) )...
4
votes
wp_head hook by page ID
If you want to use different meta tags for different pages I recommend you to use custom field values.
If you prefer to do it with conditional functions, try this:
add_action( 'wp_head','...
4
votes
Replace category titles
Well thank you for this question i did this after you posted this
function category_title( $title ){
if( is_category('name1') ){
$title = 'name1 title';
}elseif( is_category('name2') ){
$...
3
votes
How can i remove JUST the title tag from wp_head() function?
Wordpress 5.7.1 with Yoast SEO installed
Working code is:
add_filter('document_title_parts', '__return_empty_array', 10);
3
votes
Accepted
Is possible dequeue/remove style from wp_footer() hook and add on wp_head() hook?
If you look at the source code, you can see that wp_enqueue_style( 'crayon' ) is called in Crayon::enqueue_resources() which itself is called either from either Crayon::the_content() or Crayon::...
3
votes
How can I get wp_head() as a string instead of echoing it?
You can use PHP's output buffering. WIth this you can write a wrapper for the get_head() function
function wpse251841_wp_head() {
ob_start();
wp_head();
return ob_get_clean();
}
You can ...
3
votes
storing wp_head in a variable?
This function doesn't take any params, so there is no nice way of doing it.
But of course you can do this PHP way using output buffering ;)
ob_start();
wp_head();
$var = ob_get_clean();
3
votes
How can I change the <head> tag contents of individual Wordpress posts?
I would read this article, its from Yoast. I don't think you are understanding what the focus keyword feature does. This article talks about that as well as how adding the keyword meta tag is a ...
3
votes
Accepted
Custom plugin - $post_id in wp_head
Use get_queried_object_id() to get the post ID of the current page/post when viewing a single page or post, regardless of whether or not you're in the loop, or what the global $post variable happens ...
3
votes
Is it possible to modify the header with a content shortcode?
No, by the time your shortcode runs, the header has already been sent over the network, even the post title. It is too late to change things already sent, aka everything before it reached the_content.
...
2
votes
Edit title-tag in wp_head with yoast seo
I don't think adding <span itemprop> to the <title> is a good idea... If you can not use any existing tags like <h1> for that, you may consider adding meta tag: <meta itemprop="...
2
votes
Adding code to head to all pages manually
You might use the wp_head action,
That is triggered within the section of the user's template by the wp_head() function.
add_action("wp_head", "hook_js");
function hook_js() {
if(is_home() || ...
2
votes
Track down where script is being enqueued from
There is no filter when a script is registered, so it is hard to track that down. Use a full text search in all your theme and plugin files to find the source.
In addition, you can filter the URL ...
2
votes
Accepted
Auto Generated HTML source code formatting
I know it's really trivial
Yes. Yes it is.
...we are shooting to do the best job possible
Spending any time whatsoever on fixing tab indentation in your output HTML as opposed to focusing on the ...
2
votes
Remove all theme CSS & JS from wp_head (but only for 1 page template)
I modified Heinrich Held answer so this will help new people who are looking to remove, deque and de register theme styles and scripts. The following function will remove all the styles registered.
...
2
votes
Accepted
How can I get wp_head() as a string instead of echoing it?
Thanks to @Tunji's answer, I accomplished this with a more generic function:
/**
* Use output buffering to convert a function that echoes
* to a return string instead
*/
function ...
2
votes
Change default s.w.org dns-prefetch resource hint value
var_export( $hints );
gave me
array (
0 => 'https://s.w.org/images/core/emoji/2.4/svg/',
)
so that means the array key is not s.w.org but https://s.w.org/images/core/emoji/2.4/svg/.
Changing ...
2
votes
Replace category titles
You shouldn't use wp_head action - it doesn't give you a chance to modify title that is already printed. But WordPress has special hook for that: wp_title.
The code below should do the trick:
...
2
votes
Accepted
Why wp_head() function not loading style.css?
The theme functions file name must be called functions.php not function.php. This can cause your code to not implement? You must check carefully.
2
votes
Removing auto versioning of JS and loading to header
If I'm reading correctly you just want to deregister a script but failed... I think that you tried to deregister it too early( before was added ).
add_action( 'wp_print_scripts', 'example' );
...
1
vote
Different favicon on different pages
As of WordPress version 5.6, any posts or pages has options on the editor to include any custom scripts/styles and meta tags to the header of the page. So if you put your <link rel"icon" ...
1
vote
Add <meta> tags to the <head> section via functions.php
DIY SEO:
1: In the post/page editor for a page or post: Add custom field(s) (meta data) with required value(s) for your SEO meta tag(s).
e.g. Open say your "Terms &Conditions" page in the page ...
1
vote
How to load different css file for different pages
If you wish to do it in your template you can do the following.
You can learn more about the is_page function here: https://developer.wordpress.org/reference/functions/is_page/
<html lang="en"&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
wp-head × 232plugins × 29
functions × 29
actions × 26
headers × 22
css × 20
wp-enqueue-script × 18
plugin-development × 17
php × 16
javascript × 16
hooks × 16
theme-development × 15
wp-enqueue-style × 15
filters × 12
jquery × 11
customization × 10
shortcode × 9
footer × 8
html × 7
themes × 6
widgets × 5
post-meta × 5
title × 5
performance × 5
child-theme × 4