phpwordpressseohtml-meta

Customize meta tag of SEO Yoast


I am using woocommerce with seo yoast. I want on product detail page show custom taxonomy seo details if product seo is not done.

Here is the what I am trying:

function custom_metas()
{
    if(is_product()){
        global $post;
        $prod_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
        $prod_meta_desc = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
        $prod_meta_kw = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);

        if(empty($prod_meta_desc) && empty($prod_meta_title) && empty($prod_meta_kw))
        {
            $terms = get_the_terms( $post->ID, 'brand' );
            $meta   = get_option( 'wpseo_taxonomy_meta' );
            $terms = array_reverse($terms);
            if(!empty($terms))
            {
                $set_meta = 0;
                foreach($terms as $term)
                {
                    if($set_meta == 1)
                    {
                        break;
                    }
                    $term_meta_title =  $meta["brand"][$term->term_id]['wpseo_title'];
                    $term_meta_desc =  $meta["brand"][$term->term_id]['wpseo_desc'];
                    $term_meta_keywords =  $meta["brand"][$term->term_id]['wpseo_focuskw'];
                    if(!empty($term_meta_title) || !empty($term_meta_desc) || !empty($term_meta_keywords))
                    {
                        echo "coming";
                        //wpseo_replace_vars( $term_meta_title, get_post( $post_id, ARRAY_A ) )
                        apply_filters( 'wpseo_title', "tseting");
                        apply_filters( 'wpseo_metadesc', trim( $term_meta_desc ) );
                        apply_filters( 'wpseo_metakey', trim( $term_meta_keywords ) );
                        $set_meta = 1;
                    }
                }
            }
        }
    }
}
add_action("wp_head","custom_metas");

My custom taxonomy is brand. Issue is that seo details are not overwriting the existing one.

How I can achieve this?


Solution

  • I found the fix.

    In single.php file, i used filter function before header being called.

    Here is my single.php

        <?php 
    
        if ( ! defined( 'ABSPATH' ) ) {
          exit; // Exit if accessed directly
        }
        global $post, $product;
        if(is_product()){
            global $post;
            $prod_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
            $prod_meta_desc = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
            $prod_meta_kw = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);
    
            if(empty($prod_meta_desc) && empty($prod_meta_title) && empty($prod_meta_kw))
            {
                add_filter('wpseo_metadesc', 'custom_metadesc', 100,1);
                add_filter('wpseo_metakey', 'custom_metakeywords', 100,1);
            }
        }
        get_header('shop'); ?>
    

    And in theme's function.php, i defined these filter function:

    function custom_metadesc($desc){
        global $post;
        $terms = get_the_terms( $post->ID, 'brand' );
        $meta   = get_option( 'wpseo_taxonomy_meta' );
        $terms = is_array($terms) ? array_reverse($terms) : $terms;
        if(!empty($terms))
        {
            $set_meta = 0;
            foreach($terms as $term)
            {
                if($set_meta == 1)
                {
                    break;
                }
                $term_meta_desc =  $meta["brand"][$term->term_id]['wpseo_desc'];
                if(!empty($term_meta_desc))
                {
                    $desc = $term_meta_desc;
                    $set_meta = 1;
                }
            }
        }
        return trim($desc);
    }
    
    function custom_metakeywords($keywords){
        global $post;
        $terms = get_the_terms( $post->ID, 'brand' );
        $meta   = get_option( 'wpseo_taxonomy_meta' );
        $terms = array_reverse($terms);
        if(!empty($terms))
        {
            $set_meta = 0;
            foreach($terms as $term)
            {
                if($set_meta == 1)
                {
                    break;
                }
                $term_meta_keywords =  $meta["brand"][$term->term_id]['wpseo_focuskw'];
                if(!empty($term_meta_keywords))
                {
                    $keywords = $term_meta_keywords;
                    $set_meta = 1;
                }
            }
        }
        return trim($keywords);
    }