Two title tags in my header

I’m building a child theme and it currently has a very simple <head> section in header.php:

<head>
  <meta charset="<?php bloginfo( 'charset' ); ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title><?php wp_title( '|', true, 'right' ); ?></title>
  <link rel="profile" href="http://gmpg.org/xfn/11" rel="nofollow noreferrer noopener">
  <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" rel="nofollow noreferrer noopener">
  <!--[if lt IE 9]>
  <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/html5shiv.min.js"></script>
  <![endif]-->

  <?php wp_head(); ?>
</head>

I’m guessing wp_head() is responsible for the second <title> element (it appears just there in the final HTML), but other things I’ve read say this is impossible.

Should I be removing the <title> from my header.php, or should I be adding something to my functions to remove the title from wp_head() (eg. remove_action('wp_head', 'title') ?

Or should I be doing something else altogether?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 already doing this. Look in your functions.php for this line of code

add_theme_support( 'title-tag' );

As a solution, copy the parent theme header.php to your child theme and simply remove the wp_title() function from the child theme header.php

Here is also a great function to keep in mind for backwards compatibility and is useful for parent theme developers: (Taken from the codex)

 if ( ! function_exists( '_wp_render_title_tag' ) ) {
    function theme_slug_render_title() 
    {
        ?>
        <title>
            <?php wp_title( '|', true, 'right' ); ?>
        </title>
        <?php
    }
    add_action( 'wp_head', 'theme_slug_render_title' );
}

Method 2

At function.php file in your theme comment code

//add_theme_support( 'title-tag' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x