The Ultimate Title Tag Generator for WordPress
Monkeying with the title
tag is equally important and frustrating via WordPress’ native setup. I like to be able to control my browser titles separately from actual heading titles. To do this, I simply use a custom field labeled meta_title_tag
, and the following function. Paste this into your functions.php:
add_filter( 'wp_title', 'click_the_title', 20 );
function click_the_title( $title ) {
global $post;
if (is_404()) {
echo '404 Error: Page Not Found';
}
elseif (is_search()) {
echo 'Search query for ['.get_search_query().'] on '.get_bloginfo('name');
}
elseif (is_front_page()) {
echo get_bloginfo('name').' '.get_bloginfo ( 'description' );
}
elseif (is_home()) {
echo get_bloginfo('name').' Blog';
}
elseif (is_category()) {
$category_description = category_description();
$category_title = single_cat_title('',false);
if ($category_description != '') {
echo $category_title.' | '.wp_strip_all_tags($category_description);
} else {
echo wp_strip_all_tags($category_title, true);
}
}
elseif (is_archive()) {
$archive_period = 'Archive for '.get_the_date().' on '.get_bloginfo('name');
echo $archive_period;
}
elseif (is_singular('product')) {
$meta_title_tag = get_post_meta($post->ID, 'the_title_tag', true);
if ($meta_title_tag != '') {
echo $meta_title_tag;
}
else {
echo 'Buy '.get_the_title().' on '.get_bloginfo('name');
}
}
elseif (is_singular()) {
$meta_title_tag = get_post_meta($post->ID, 'the_title_tag', true);
if ($meta_title_tag != '') {
echo $meta_title_tag;
}
else {
echo get_the_title().' on '.get_bloginfo('name');
}
}
else {
echo get_bloginfo('name').' '.get_bloginfo('description');
}
}
Just make sure your theme’s header.php file has the following in the actual title tag:
<title><?php wp_title(); ?></title>
Up Next: How to Link to Your Original High Quality Photos in WordPress