Show all attributes and values in short description?
-
Hello,
In Rankmath, I am trying to set the Short description meta for Woocommerce products. By default, it is except.
However, I would like to show all of the attributes values, usually, these attributes and values are in the Additional information tab on the product page at frontend.
so, could you please let us know what is the right code to set those value as short description meta? %”customterm Pa_additional_information”% ?
Is there an online document to show a list of supported Woocommerce attributes codes?
Thanks so much
- This topic was modified 1 year ago by alexliii.
-
Hello @alexliii,
Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.
All our variables can be seen here: https://rankmath.com/kb/variables-in-seo-title-description/
At the moment, our plugin is limited to adding the product attribute as we only have selected attributes such as the
%wc_brand%
.You may refer to this guide on how to add the code to your website: https://rankmath.com/kb/wordpress-hooks-actions-filters/
If you have custom attributes, you can try creating a custom variable that you can use for your meta title:
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_attribute', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_attribute', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_attribute_call_back(), ], 'rankmath_product_attribute_call_back' ); }); function rankmath_product_attribute_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'ATTRIBUTE_KEY', true ); return $custom_attribute_value; }
After applying the code, you can use the variable?%rankmath_product_attribute%
?in your meta titles.Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hello,
Greatly thanks for code snippits, and I tried those code, but it not work as expected.
I would like to show more details to avoid misunderstanding:
- # We have Color, Size, Material, Capacity as products attributes. For specific attribute, it might include lots of vaules: https://prnt.sc/MwGKqmuUclBW
- However, for specific product, there might be limited variables included, like this way:https://prnt.sc/wpxuNYz9dxQC
- Usually, these attributes and values are displayed on Additional information Tab: https://prnt.sc/queguO6Poura
What we expect is to show all of attributes and values in the Single Product Description in Rankmatch here: https://prnt.sc/sjYb_eqMARL1
However, the “Single Product Description” is product short description: https://prnt.sc/HX2xKljVXYza, according to Rankmath official document at https://rankmath.com/kb/titles-and-meta/?utm_source=Plugin&utm_medium=Options%20Panel%20Meta%20Products%20Tab&utm_campaign=WP#products
Here is what I did as per your guide:
1# Put the code snippets into the plugin of Code Snippets and set it work both backend and frontend: https://prnt.sc/UA2fRbbKH9ac
2# Then, I input %rankmath_product_attribute% in the product description in Rankmath:
But, in meta name=”description”, and meta property=”og:description” content, it still show the product default descriptions:
Questions:
1# Are there anything I missed please?
2# Could you please show a specific example to show attributes Color, size, material, capacity in the product short desciption? so that we can show more specific attributes and values to Google search?
Thanks
Hello @alexliii,
You will have to replace the ATTRIBUTE_KEY in the filter with the actual attribute key. You will also have to create different variables for each attribute. Here are a couple of examples:
For Color:
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_color', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_color', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_color_call_back(), ], 'rankmath_product_color_call_back' ); }); function rankmath_product_color_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_color', true ); return $custom_attribute_value; }
Once added, you can use the %rankmath_product_color% variable to add the value of the color attribute.
For Size:
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_size', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_size', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_size_call_back(), ], 'rankmath_product_size_call_back' ); }); function rankmath_product_size_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_size', true ); return $custom_attribute_value; }
You can use %rankmath_product_size% variable once you have added the filter.This is the order we follow to generate meta descriptions for WooCommerce products:
- Content from SEO Description field
If that is missing, then:
- WooCommerce Excerpt or Product Short Description
If that is missing, then:
- Template From General Settings in WordPress Dashboard > Rank Math > Titles & Meta > Products
If that is missing, then:
- Auto generated Content from the product page (long description)
Since you are adding the description in the Titles & Meta, your short description will override that. Please add the filter given below to add the description from the global settings. Please note that the filter will add the correct description in the front end but you will still see the short description in the snippet editor of the meta box.
add_action( 'rank_math/frontend/description', function( $generated ) { if ( ! is_product() ) { return $generated; } global $post; $desc = RankMath\Helper::get_settings( "titles.pt_product_description" ); $desc = RankMath\Helper::replace_vars( $desc, $post ); return empty( $desc ) ? $generated : $desc; });
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.- This reply was modified 1 year ago by Rank Math Support. Reason: Edited code
Hello,
Great thanks.
I tried in the following way, but it does not work, and it would be great appreciated if there is anything wrong with my setting, and please do not hesitate to correct me.
Again, to avoid misunderstand, please allow us to clarify what we expect to achieve: Assign product attributes into the meta description.
And here is what I did:
- Step one: I enabled the following three code snippts:
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_color', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_color', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_color_call_back(), ], 'rankmath_product_color_call_back' ); }); function rankmath_product_color_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_color', true ); return $custom_attribute_value; }
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_size', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_size', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_size_call_back(), ], 'rankmath_product_size_call_back' ); }); function rankmath_product_size_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_size', true ); return $custom_attribute_value; }
add_action( 'rank_math/frontend/description', function( $generated ) { if ( ! is_product() ) { return $generated; } global $post; $desc = RankMath\Helper::get_settings( "titles.pt_product_description" ); $desc = RankMath\Helper::replace_vars( $desc, $post ); return empty( $desc ) ? $generated : $desc; });
2.Step Two: add these two value into Rankmath–> Title&Meta–> Product–>Single Product Title–>Single Product Description:
%rankmath_product_color%
%rankmath_product_size%
Please check the screenshot:
But, there will nothing output at frontend on <meta property=”og:description” content=
So, what did I missed please?
Hello @alexliii,
Currently you are calling the
get_post_meta()
function with the attribute value incorrectly.By default, the attributes on WooCommerce are saved in the postmeta table with the prefix “attribute” like so:
attribute_pa_color
Please update your code accordingly so that the correct values can be retrieved from the database.
Don’t hesitate to get in touch if you have any other questions.
Hello,
Great thanks.
I tried these codes, but no lucky:
1#
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_color', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_color', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_color_call_back(), ], 'rankmath_product_color_call_back' ); }); function rankmath_product_color_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_color', true ); return $custom_attribute_value; }
2#
add_action( 'rank_math/vars/register_extra_replacements', function(){ rank_math_register_var_replacement( 'rankmath_product_size', [ 'name' => esc_html__( 'Author name', 'rank-math' ), 'description' => esc_html__( 'Author description', 'rank-math' ), 'variable' => 'rankmath_product_size', /* the actual variable: %rankmath_product_attribute% */ 'example' => rankmath_product_size_call_back(), ], 'rankmath_product_size_call_back' ); }); function rankmath_product_size_call_back(){ $product_id = get_the_ID(); $custom_attribute_value = get_post_meta( $product_id, 'pa_size', true ); return $custom_attribute_value; }
3#
add_action( 'rank_math/frontend/description', function( $generated ) { if ( ! is_product() ) { return $generated; } global $post; $desc = RankMath\Helper::get_settings( "titles.pt_product_description" ); $desc = RankMath\Helper::replace_vars( $desc, $post ); return empty( $desc ) ? $generated : $desc; });
Then, add
%rankmath_product_color%
%rankmath_product_size%
into Rankmath–> Title&Meta–> Product–>Single Product Title–>Single Product Description:
What did I miss please?
Hello @alexliii,
Those are exactly the same you shared previously and as we mentioned, the attributes are not saved in the format you have currently.
The keys for the attributes are prefixed by the “attribute” word in the following format
attribute_pa_color
, for example.You need to change the code in the callback functions to follow the same structure when calling for the meta key inside the
get_post_meta()
function.Don’t hesitate to get in touch if you have any other questions.
Hello,
Sorry, I correct code:
attribute_pa_color:
attribute_pa_size:
and the code: Auto generated Content from the product page
In rankmath:
But, it does not work:(
- This reply was modified 1 year ago by alexliii.
Hello @alexliii,
Please try this code instead:
add_action('rank_math/vars/register_extra_replacements', function () { rank_math_register_var_replacement( 'rankmath_product_color', [ 'name' => esc_html__('Author name', 'rank-math'), 'description' => esc_html__('Author description', 'rank-math'), 'variable' => 'rankmath_product_color', 'example' => rankmath_product_color_call_back(), ], 'rankmath_product_color_call_back' ); }); function rankmath_product_color_call_back(){ $theProduct = wc_get_product(get_the_ID()); $custom_attribute_value = $theProduct->get_attribute('pa_color'); return $custom_attribute_value; }
You can do the same for the rest of the attributes.
Let us know how that goes. Thank you.
- The topic ‘Show all attributes and values in short description?’ is closed to new replies.