WordPress: Add Meta Title If There Is No Title Dedined

- 1 answer

I want to add a meta title in Yoast if there is no title set in the product.

To do this, I'm using the following function:

add_filter( 'wpseo_title', 'wpseo_change_product_title' );
function wpseo_change_product_title( $title ) {
    if ( is_product() ) {
        // My function to generate the title

    return $title;

But I only want to set the title if there is no title defined in the WooCommerce product.

I tried to check if $title is empty by wrapping the whole function in an if statement:

if ( empty($title)) {
    // function

But it has no effect. Do I miss something?

I saw that the Yoast SEO title is never emtpy. Yoast adds some placeholder stuff. Maybe that's the reason that there is no way to check it?



The problem is that when you try to empty the Yoast SEO title in the product, Yoast will set the title again after saving because of the default product template setting in Yoast.

To fix this you need to delete the Yoats SEO default title for products by following this documentation from Yoast:

After that your check will be working if you delete the SEO title in each product.