• Resolved kkj

    (@kkj)


    Hi,

    RankMath is adding product schmea to product pages, and I would like to add some other schema into that. I have a custom meta filed called “schema”, how can I add the content from this field to RankMaths product schema? I know how to add it to wp_footer, but I would like to add it nested into RankMath schema.

    Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @kkj,

    Thank you for contacting Rank Math support.

    You can modify and use the following filter to extend the Product Schema:

    add_filter( "rank_math/snippet/rich_snippet_product_entity", function( $entity ) { 	return $entity;});

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Thread Starter kkj

    (@kkj)

    Thank you, I am a bit confused though on how to add it it correctly. I am trying to add FAQ schema, and the content/value of my field is for example

    { “@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [ { “@type”: “Question”, “name”: “Question1”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “Answer1” } }, { “@type”: “Question”, “name”: “Question2”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “Answer2” } } ] }

    So how would I add this?

    add_filter('rank_math/snippet/rich_snippet_product_entity',	function ($entity) {
    $faq = get_post_meta(get_the_ID(), 'customfield', true);
    $entity['FAQPage'] = array(
    '@type' => 'FAQPage'
    );
    return $entity;
    });

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @kkj,
    ?
    The FAQ Schema can’t be used inside the Product Schema. You will have to add it using the following filter:?https://rankmath.com/kb/filters-hooks-api-developer/#extend-json-ld-data
    ?
    Please share some screenshots of the custom field content so we can suggest how to add the FAQ Schema. You can use a tool like?https://imgur.com/?to share screenshots.
    ?
    Looking forward to helping you.

    Thread Starter kkj

    (@kkj)

    Hi,

    thank you! A screenshot is here.

    Also, I am having trouble adding other custom fields like one for brand or mpn. Could you help me with the correct code as well?

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    global $product;
    if ( ! is_a( $product, 'WC_Product' ) ) {
    return $entity;
    }
    $entity['brand']['@type'] = 'Brand';
    $entity['brand'] = get_post_meta( $post->ID,'brand', true );
    $entity['mpn'] = get_post_meta( $post->ID,'mpn', true );

    return $entity;
    });
    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @kkj,

    Please refer to the following code to modify the brand and mpn property to your existing product schema:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    global $post;

    $entity['brand']['@type'] = 'Brand';
    $entity['brand'] = get_post_meta( $post->ID,'brand', true );
    $entity['mpn'] = get_post_meta( $post->ID,'mpn', true );

    return $entity;
    });

    If you wish to apply the mpn property to each variation of the product, you may refer to the code below instead:

    add_filter('rank_math/snippet/rich_snippet_product_entity', function ($entity) {
    global $post;
    if (!is_product()) {
    return $entity;
    }

    $entity['brand']['@type'] = 'Brand';
    $entity['brand'] = get_post_meta( $post->ID,'brand', true );

    $product = wc_get_product(get_the_ID());
    if (!$product->is_type('variable')) {
    return $entity;
    }

    $variations = $product->get_available_variations();

    if (!empty($variations)) {
    $offers = [];
    foreach ($variations as $variation) {
    $price_valid_until = get_post_meta($variation['variation_id'], '_sale_price_dates_to', true);

    $offers[] = [
    '@type' => 'Offer',
    'description' => strip_tags($variation['variation_description']),
    'price' => $variation['display_price'],
    'priceCurrency' => get_woocommerce_currency(),
    'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
    'priceValidUntil' => $price_valid_until ? date_i18n('Y-m-d', $price_valid_until) : '2025-12-31',
    'itemCondition' => 'https://schema.org/NewCondition',
    'url' => $variation->get_permalink(),
    'mpn' => get_post_meta( $post->ID,'mpn', true )
    ];
    }
    }

    $entity['offers'] = $offers;
    return $entity;
    });

    As for the FAQPage schema, please refer to the code below:

    add_filter('rank_math/json_ld', function ($data, $jsonld) {
    $jsonField = get_post_meta(get_the_ID(), 'customfield', true);
    $faq_schema = json_decode($jsonField, true);
    $data['faqs'] = $faq_schema;

    return $data;
    }, 10, 2);

    Let us know if that helps.

    Thread Starter kkj

    (@kkj)

    That helps very much, thank you. One last thing I just noticed: if I wanted to remove one specific part of the schema, for example the author url, but not the full author, how does this work? I tried

    add_filter("rank_math/snippet/rich_snippet_article_entity", function ( $entity ) {
    if ( isset( $entity['author'] ) ) {
    unset( $entity['author']['url']);
    return $entity;
    }
    return $entity; });

    This does not work unfortunately. Thanks for your help!

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @kkj,

    The entity for the author in the Article Schema only contains the ID and the name because the data is referenced from the Person Schema on the page via the ID.

    To remove the URL you need to edit the actual Person Schema with a different filter. For example, the following code snippet would remove the URL from that entity:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    unset($data['ProfilePage']['url']);
    return $data;
    }, 99, 2);

    Don’t hesitate to get in touch if you have any other questions.

    Thread Starter kkj

    (@kkj)

    Thank you very much!

    Plugin Support Rank Math Support

    (@rankmathsupport)

    Hello @kkj,
    ?
    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
    ?
    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on
    https://www.ads-software.com/support/plugin/seo-by-rank-math/reviews/#new-post
    ?
    about your overall experience with Rank Math? We appreciate your time and patience.
    ?
    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
    ?
    Thank you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.