Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.76k stars 887 forks source link

AMP Title Tag Missing on Some Themes #15100

Open TwisterMc opened 4 years ago

TwisterMc commented 4 years ago

Please give us a description of what happened.

On some themes, the AMP version of posts do not have title tags with the latest version of Yoast Premium. If I disable Yoast Premium, the title tags come back.

When I tested with Twenty Twenty it seem to work fine. But other themes such as Twenty Eleven are broken.

It feels like something is not backwards compatible when it comes to AMP pages. Non-AMP pages work as expected.

Please describe what you expected to happen and why.

I expect the post title tags to show.

How can we reproduce this behavior?

  1. Install AMP (official) Plugin
  2. Install Yoast Premium
  3. Install Twenty Eleven
  4. Visit the AMP version of a post.

Technical info

Used versions

Djennez commented 4 years ago

Confirmed. The twentyeleven theme does not have support for the title-tag functionality, this is the main reason this problem is happening.

Basically, AMP is overwriting the set theme to output on their special AMP pages. They request the title from WordPress core where we hook into. But we check the installed theme for the title-tag functionality and if that is not present, we don't output a title presenter. Because we don't do this, WP Core does not seem to receive a title tag and therefore AMP does not get one to output on their page.

I don't see a general way for us to "know" if the main theme gets overwritten with something that does support dynamic titles. Case-specific we can add AMP as an exception in the check for title-tag.

TwisterMc commented 4 years ago

Thanks for confirming. You're right, it's not clear where the issue falls on this.

A quick fix for anyone facing this problem is adding add_theme_support( 'title-tag' ); to your theme's function.php file.

Djennez commented 4 years ago

@TwisterMc

A quick fix for anyone facing this problem is adding add_theme_support( 'title-tag' ); to your theme's function.php file.

Take care with that, as this might introduce double <title> tags on non-AMP pages if the theme is also outputting its own titles.

TwisterMc commented 4 years ago

Shoot! It does! Now I need to find that happy middle ground.

Djennez commented 4 years ago

Maybe you can use our presenters filter as a workaround, like this for example:

add_filter( 'wpseo_frontend_presenter_classes', 'yoast_amp_title' );

function yoast_amp_title( $presenters){
    if ((array_search('Yoast\WP\SEO\Presenters\Title_Presenter', $presenters)) == false) {
        $presenters[] = 'Yoast\WP\SEO\Presenters\Title_Presenter';
    }
    return $presenters;
}

Now this is very crude, and just checks if the Title presenter is set (it is unset before this filter is applied because of the above mentioned theme function). So if it is not set, it will set it. You should probably add some more logic to determine if you're really on a page for which you want to force-add the title tag.

westonruter commented 4 years ago

To prevent duplicating title tags on non-AMP pages, this this modification of the above workaround would be needed:

add_filter(
    'wpseo_frontend_presenter_classes',
    function ( $presenters ) {
        if (
            function_exists( 'is_amp_endpoint' )
            &&
            is_amp_endpoint()
            &&
            array_search( 'Yoast\WP\SEO\Presenters\Title_Presenter', $presenters, true ) === false
        ) {
            $presenters[] = 'Yoast\WP\SEO\Presenters\Title_Presenter';
        }
        return $presenters;
    }
);
TwisterMc commented 4 years ago

Thanks everyone.

jacraven commented 3 years ago

Thanks @westonruter – this solution worked for me as well!