Populating WooCommerce Product Tags into Meta Description

#954237
  • Resolved Hindi stack
    Rank Math free

    Hello,

    I am looking for guidance on how to populate ‘product tags’ into the meta description editor.

    I would appreciate any examples or resources you could share.

    Thank you for your time and assistance. I look forward to your response.

    Best regards,
    Hindistack

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    We apologize for the issue you are facing. Unfortunately, the context provided does not include specific instructions on how to populate WooCommerce product tags into the meta description editor.

    We recommend replying to this ticket with your request, and a human agent from the Rank Math Support team will be able to assist you further with detailed guidance or examples.

    Thank you for raising this concern, and we appreciate your understanding!

    Hello,
    I tried this with AI, but I am facing a critical site issue.
    Please check this code and let me know how I can fix it.

    /**
     * Register custom Rank Math variables for WooCommerce product tags.
     */
    add_action('rank_math/vars/register_extra_replacements', function () {
        // Register basic WooCommerce product tags variable.
        rank_math_register_var_replacement(
            'wc_tags',
            [
                'name'        => esc_html__('Product Tags', 'rank-math'),
                'description' => esc_html__('Displays tags of the current WooCommerce product.', 'rank-math'),
                'variable'    => 'wc_tags',
                'example'     => wc_tags_callback(),
            ],
            'wc_tags_callback'
        );
    
        // Register advanced WooCommerce product tags variable.
        rank_math_register_var_replacement(
            'wc_tags_advanced',
            [
                'name'        => esc_html__('Product Tags (Advanced)', 'rank-math'),
                'description' => esc_html__('Displays WooCommerce product tags with options like limit, separator, and exclude.', 'rank-math'),
                'variable'    => 'wc_tags_advanced(limit=3&separator= | &exclude=12,23)',
                'example'     => wc_tags_advanced_callback('limit=3&separator= | &exclude=12,23'),
            ],
            'wc_tags_advanced_callback',
            true // Allows passing dynamic parameters.
        );
    });
    
    /**
     * Basic Product Tags Callback Function.
     */
    function wc_tags_callback() {
        if (function_exists('wc_get_product') && is_product()) {
            $product_id = get_the_ID(); // Get the current product ID.
            $tags = wp_get_post_terms($product_id, 'product_tag', ['fields' => 'names']); // Fetch product tags.
    
            return !empty($tags) ? implode(', ', $tags) : esc_html__('No Tags', 'rank-math'); // Return tags or fallback.
        }
    
        return esc_html__('Not a Product Page', 'rank-math'); // Fallback for non-product pages.
    }
    
    /**
     * Advanced Product Tags Callback Function.
     *
     * @param string $options Options string in the format <code>limit=3&separator= | &exclude=12,23</code>.
     * @return string Formatted product tags based on the given options.
     */
    function wc_tags_advanced_callback($options = '') {
        if (!function_exists('wc_get_product') || !is_product()) {
            return esc_html__('Not a Product Page', 'rank-math');
        }
    
        // Parse options
        $options = trim(str_replace(['wc_tags_advanced(', ')'], '', $options)); // Remove variable wrapping.
        parse_str($options, $args);
    
        // Default values
        $limit = isset($args['limit']) ? (int) $args['limit'] : 0;
        $separator = isset($args['separator']) ? $args['separator'] : ', ';
        $exclude = isset($args['exclude']) ? array_map('intval', explode(',', $args['exclude'])) : [];
    
        // Get product tags
        $product_id = get_the_ID();
        $tags = wp_get_post_terms($product_id, 'product_tag', ['fields' => 'all']);
    
        if (is_wp_error($tags)) {
            return esc_html__('Error fetching tags', 'rank-math');
        }
    
        // Filter excluded tags
        if (!empty($exclude)) {
            $tags = array_filter($tags, function ($tag) use ($exclude) {
                return !in_array($tag->term_id, $exclude, true);
            });
        }
    
        // Limit tags
        if ($limit > 0) {
            $tags = array_slice($tags, 0, $limit);
        }
    
        // Format tag names
        $tag_names = wp_list_pluck($tags, 'name');
    
        return !empty($tag_names) ? implode($separator, $tag_names) : esc_html__('No Tags', 'rank-math');
    }

    Hello,

    Can you please confirm if you want to use the product tags on the product meta description? If so, for the tag title, you can use %customterm(product_tag)% and for the tag description, you can use %customterm_desc(product_tag)% variables in the meta description field.

    However, please note that the variables may not be visible while editing the products as it only works when your products load in a browser. You can check the published/saved product’s meta description via its source code.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Yes, I want the tag title, and this is working: %customterm(product_tag)%. However, this variable only renders the first tag title. I want it to render the first three tags, separated by commas, if there are five tags.

    Hello,

    In this case, you can use the following filter code to create a custom variable:

    add_action('rank_math/vars/register_extra_replacements', function () {
        rank_math_register_var_replacement(
            'product_tags',
            [
                'name'        => esc_html__('Product Tags', 'rank-math'),
                'description' => esc_html__('Returns the first three WooCommerce product tags, separated by commas.', 'rank-math'),
                'variable'    => 'product_tags',
                'example'     => 'Tag1, Tag2, Tag3',
            ],
            'product_tags_callback'
        );
    });
    
    function product_tags_callback()
    {
        if (!function_exists('wc_get_product')) {
            return ''; // Return an empty string if WooCommerce is not active.
        }
    
        $product = wc_get_product(get_the_ID());
        if (!$product) {
            return ''; // Return an empty string if no product is found.
        }
    
        $tags = wp_get_post_terms($product->get_id(), 'product_tag', ['fields' => 'names']);
        if (empty($tags)) {
            return ''; // Return an empty string if no tags are found.
        }
    
        // Get the first three tags and join them with commas.
        $tags = array_slice($tags, 0, 3);
        return implode(', ', $tags);
    }
    

    Here’s how you can add filter/hook to your WordPress site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    After this, you can use the %product_tags% variable in the SEO description or single product description field.

    Please note that the filter may not be visible while editing the products as it only works when your products load in a browser. You can check the published/saved product’s meta description via its source code.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Hindi stack
    Rank Math free

    Thank you very much!!

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this ticket.