Help: Adding meta tags with Wp_head
-
My goal: Add in meta tags to a theme’s header through the function file.
Why?: Only reason is for my sake, keeping the header.php fairly clean and using functions to put in the stuff I’d rather not think about.my code:
function d4_head() { echo '<meta charset="' . bloginfo( "charset" ) . '" />' ; echo '<link rel="profile" href="http://gmpg.org/xfn/11" />'; echo '<link rel="pingback" href="' . bloginfo( "pingback_url" ) . '" />'; // Favicon echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/img/favicon.ico" />'; // Jquery - Google's if (!is_admin()) { wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3', true); wp_enqueue_script('jquery'); } } add_action('wp_head', 'd4_head');Problem: What actually happens is that it puts it right at the very top of the page, in the body tag, not the head.
Research: I’ve read that echoing into the WP-head is not best practice and in my case, doesn’t work at all. ‘Return’ doesn’t work.
I found wp_enqueue_script will work fine for CSS & Javascripts… however the remaining is the meta data.
I’ve also tried adding
<?php d4_head; ?>into the head itself, but alas… nothing.
-
can you send a URL to your website? and paste in the contents of your
header.phpfile if it’s not too long or put it in http://pastebin.com/ if it’s quite long.The wp_head(); function – which runs the “wp_head” hook – is usually placed right before the closing
</head>tag but in your case I think it’s placed right AFTER the opening<body>tag or something.just to notify me of followup emails
Nope, wp_head is right before closing head.
<!DOCTYPE html><html <?php language_attributes(); ?> class="<?php if (function_exists('css_browser_selector')) { echo css_browser_selector(); } else { echo 'off'; } ?>"> <head> <title><?php wp_title( '|', true, 'right' ); ?></title> <?php d4_head(); ?> <?php wp_head(); ?> </head> <body <?php body_class(); ?>>You can see here, I’m currently trying to just echo the function straight in the the head with d4_head();. The Functions.php, no longer has the add_action and I get the same result, any ‘echo’ actually echoes inside the body tag…
Currently, this is a barebones theme I’m developing and there is no live site to view.
Ah, the trouble was with the bloginfo() function. The function itself doesn’t return, it echoes. So I was double echoing. I cleaned that up and it works perfectly.
here’s the header.php (You can see the minimalistic file I was going for)
<!DOCTYPE html><html <?php language_attributes(); ?> class="<?php if (function_exists('css_browser_selector')) { echo css_browser_selector(); } else { echo 'off'; } ?>"> <head> <title><?php wp_title( '|', true, 'right' ); ?></title> <?php wp_head(); ?> </head> <body <?php body_class(); ?>>Here’s the function:
function d4_head() { echo '<meta charset="'; bloginfo( "charset" ); echo '" />' ; echo '<link rel="profile" href="http://gmpg.org/xfn/11" />'; echo '<link rel="pingback" href="'; bloginfo( "pingback_url" ); echo '" />'; // Favicon echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/img/favicon.ico" />'; // Jquery - Google, then wordpress's if (!is_admin()) { wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3', true); wp_enqueue_script('jquery'); } // Theme CSS echo '<link rel="stylesheet" type="text/css" media="all" href="'.get_bloginfo( 'stylesheet_url' ).'" />'; } add_action('wp_head', 'd4_head');
The topic ‘Help: Adding meta tags with Wp_head’ is closed to new replies.