Customizing WP Schema Markup Output

Modified on Mon, 23 Dec 2019 at 03:32 PM

To help users further customize their schema markup generated by our Wordpress plugins, we provide two filters. Wordpress Filters allow users to check or replace content such as Schema markup within plugins or themes. For our Base Plugin, we have created the following filters

  • hunch_schema_markup 
  • hunch_schema_markup_breadcrumb 
  • hunch_schema_woocommerce_productschema

Both filters provide an argument $PostType, so as to further allow plugins to filter output based on Post Types. 


Note: Typically, Wordpress filters are added to your theme's functions.php. See https://developer.wordpress.org/themes/basics/theme-functions/#what-is-functions-php for more information. 


Blog Posts

For custom BlogPosting Author:

function schema_markup_custom_author ( $schema_markup, $schema_markup_type, $post, $post_type ) {
  
  if ( ! empty( $schema_markup ) ) {
    $schema_markup = json_decode( $schema_markup );
    
    if ( $schema_markup->{'@type'} == 'BlogPosting' ) {
      $schema_markup->author = array(
        '@type' => 'Person',
        '@id' => 'http://example.com/author/jone-doe/#Person',
        'name' => 'Jone Doe',
        'url' => 'http://example.com/author/jone-doe/',
      );
      $schema_markup = json_encode( $schema_markup );
    }
  } 

  return $schema_markup;
}
add_filter( 'hunch_schema_markup', 'schema_markup_custom_author', 10, 4 );


Products

With WooCommerce Products you can modify WooCommerce Product schema markup using the hunch_schema_woocommerce_productschema filter.

For example Based on Products in a certain Category “PreOrder”, custom set the "offers > Offer > availability”.  Note, you would need to adjust the below with the ID of the category and fill in the value as appropriate. 


function schemaAdjustPreorder ( $product_schema ) {
    // Incorporate “PreOrder” availability on Products in a certain Product Category
   global $post;
   $product_category_ids = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'ids') );

   // Replace the ID of the product category as appropriate. 
   if ( in_array( 1036, $product_category_ids ) ) {
      $product_schema['offers']['availability'] = 'http://schema.org/PreOrder';
   }

    return $product_schema;
}
add_filter( 'hunch_schema_woocommerce_productschema', 'schemaAdjustPreorder' );


Products Remove Page Default Article Markup

With Wordpress you can remove the default article markup for page type pages using the schema_markup_custom_page filter.


function schema_markup_custom_page ( $schema_markup, $schema_markup_type, $post, $post_type ) {  

     if ( ! empty( $schema_markup ) ) {    
           $schema_markup = json_decode( $schema_markup );    

           if ( $schema_markup->{'@type'} == 'Article' && $schema_markup_type == 'Default' ) {      
                     return ''; 
           }  
    }  
    
return $schema_markup;
}
add_filter( 'hunch_schema_markup', 'schema_markup_custom_page', 10, 4 );


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article