Collect attribute using JSON-LD

#8791
  • Resolved Paul
    Rank Math free

    Hi,

    I have seen this code in the knowledge base, but I need some help with how to edit it.

    I have created an attribute called MPN and I would like this filter to pick it up and output as json-ld so GSC will recognise it as the MPN field for my products.

    /**
     * Collect data to output in JSON-LD.
     *
     * @param array  $unsigned An array of data to output in json-ld.
     * @param JsonLD $unsigned JsonLD instance.
     */
    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    	return [];
    }, 10, 2);

    Could you help me to edit the above code to get the MPN attribute please.

    Cheers,

    Paul

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

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    I recommend using this filter instead and then write some logic to grab the created attribute from the current product in the loop:

    /**
     * Filter to add Brand Name for Products.
     *
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        $entity['mpn'] = '';
        return $entity;
    });

    I hope this info helps. Thank you.

    ​​​​​​​

    Paul
    Rank Math free

    Thanks Michael,

    Is there anyone at RankMath that would know how to write some logic for that filter? as I don’t have enough knowledge in that area to know what to do.

    Cheers,

    Paul

    Hi Paul,

    Yes we would be happy to help.
    Please edit the first post on this ticket and include your WP logins in the designated Sensitive Data section.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Paul
    Rank Math free

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hi there,

    Thanks for getting back to us.

    The custom attribute is already added to the product schema as the brand as shown below:
    IMG

    I don’t think that it is necessary to add some additional code.

    Thank you.

    Paul
    Rank Math free

    I’m trying to add the MPN attribute, not the brand.

    Hi Paul,

    Apologies for that. I have added this code to your theme to grab the custom MPN number and add it to the product schema:

    
    /**
     * Filter to add custom MPN number for Products.
     *
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
        if ( is_singular( 'product' ) ) {
    		
    	    $entity['mpn'] = get_post_meta( get_the_ID(), '_mpn', true );
    		
    	}
    	return $entity;
    });

    I hope this info helps. Thank you.

    Paul
    Rank Math free

    Thank you Michael,

    That looks like it worked and the MPN attribute is now showing in GSC.

    Thanks for your help, I appreciate it.

    Cheers,

    Paul

    Hello,

    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://wordpress.org/support/plugin/seo-by-rank-math/reviews/?rate=5#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)

The ticket ‘Collect attribute using JSON-LD’ is closed to new replies.