Title Tag within WordPress

I try to set it Title Tag. I have an issue how to make it in the correct way. Most of WordPress themes support title-tag but new theme throws an error when validated:

Need help.

    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

Should we set it inside functions to detect ‘title-tag’?

Hi @toplisek, what error exactly are you getting? PHP does indeed allow return statements outside functions, in which case the execution of the entire current script is getting stopped (which however might certainly cause your code not to run as intended, or even induce other errors).

https://www.php.net/manual/en/function.return.php

In any case, it’s most probably not the theme throwing an error here. You might try putting your title tag logic inside the if block, rather than using an early return guard… so this:

if ( ! current_theme_supports( 'title-tag' ) ) {
    return;
}

// Your title-tag logic

Would become:

if ( current_theme_supports( 'title-tag' ) ) {
    // Your title-tag logic
}

PS: Or actually using a function:

function my_title_tag_logic() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

    // The title-tag logic
}

// Somewhere else
my_title_tag_logic();

Yes, exactly and RETURN will push WordPress has an error.

  1. Which line inside WordPress and its file functions.php will push
title-tag
  1. Which line should be inside PHP/theme to show TITLE?
  2. How to manage and echo only TITLE without right part in this case?

It is a new theme set up.

How to set inside a new custom theme forced

The title-tag 

It does not recognize any TITLE-TAG as it seems it should be set inside functions.php.

To add support for the title tag in your custom WordPress theme, you can use the add_theme_support() function in your theme’s functions.php file. Here’s how you can do it:

function theme_slug_setup() {
   add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_slug_setup' );

In this code, theme_slug_setup is a custom function that calls add_theme_support( 'title-tag' ) to enable support for the title tag. This function is hooked into the after_setup_theme action, which is triggered after the theme is loaded.

After adding this code to your functions.php file, WordPress will automatically manage the title tag for your theme, and you should see the correct title tags in your pages.

Remember to replace theme_slug_setup with a unique function name that matches your theme’s naming conventions to avoid conflicts with other themes or plugins.

1 Like

You are excellent developer. Thank you for the message!
So, this will be the correct way within function:

function theme_slug_titles_setup() {
   add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_slug_titles_setup' );

and within a theme we set it up:

if (is_front_page() && is_home()) { // Default homepage ( both the front page and the recent posts page)
    echo ('Default homepage');

} elseif (is_front_page()) { // Static homepage
    echo ('Static homepage');

} elseif (is_home()) { // Blog page
    echo ('Blog page');

} else {

    //$title = wp_get_document_title();
    //$title = wp_title( '', false, 'right' );
    //echo $title;
    //the_title('<h1 class="">', '</h1>', true);

    function _wp_render_title_tag()
    {
        if (!current_theme_supports('title-tag')) {
            return;
        }
        echo wp_get_document_title(); //the_title();
    }
}

I am glad it helped :slight_smile: !!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.