The right way to add title in WordPress theme, learn how to add title to WordPress blog, easy way and simple, with full example code.
How To Add Title In WordPress Theme
We have way for WordPress version 4.1 and higher, and way for older versions, we will learn how to use both inside WordPress theme. For version 4.1 and higher: we need add_theme_support(‘title-tag’) function and after_setup_theme action. For older versions: we need wp_title() function and wp_title filter.
Add Title To WordPress Theme
Just copy function code and paste it in your “functions.php” file:
function WPTime_add_title_to_theme(){ add_theme_support( 'title-tag' ); // title for version 4.1 and higher } add_action( 'after_setup_theme', 'WPTime_add_title_to_theme' );
Now add this function, this function to add title for WordPress older versions:
// Set Title for older WordPress versions if ( !function_exists( '_wp_render_title_tag' ) ) { function WPTime_theme_title() { ?> <title><?php wp_title( '-', true, 'right' ); ?></title> <?php } add_action( 'wp_head', 'WPTime_theme_title' ); function WPTime_filter_title( $title ){ if( empty( $title ) && ( is_home() || is_front_page() ) ) { return get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' ); } else{ return $title . get_bloginfo( 'name' ); } } add_filter( 'wp_title', 'WPTime_filter_title' ); }
Do not add title in “header.php” file, like this:
<!DOCTYPE html> <html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#"> <head> <meta charset="<?php bloginfo('charset'); ?>"> <title><?php wp_title( '-', true, 'right' ); ?></title> <?php wp_head(); ?> </head>
If you want to add wp_title() function in “header.php” file, no problem but do not use add_theme_support(‘title-tag’) function, just add wp_title() function in “header.php” file and add this filter in “functions.php” file:
function WPTime_header_title_filter( $title ){ if( empty( $title ) && ( is_home() || is_front_page() ) ) { return get_bloginfo( 'name' ) . ' - ' . get_bloginfo( 'description' ); } else{ return $title . get_bloginfo( 'name' ); } } add_filter( 'wp_title', 'WPTime_header_title_filter' );
Enjoy.