Customizing WP Schema Markup Output

Modified on Fri, 20 Jun at 3:42 PM

To help users further customize their schema markup generated by our Wordpress plugins, we provide a number of 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_markup_website

All filters for the base plugin provide an argument $PostType, so as to further allow plugins to filter output based on Post Types. 


For the Advanced Plugin, we have created the following filters to augment WooCommerce resources:

  • hunch_schema_woocommerce_productschema
  • hunch_schema_woocommerce_breadcrumb
  • hunch_schema_woocommerce_productcategoryschema


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 );


Remove all Default Markup

For disabling Default markup from all post and pages, include the following filter. You may use this in the scenario that you only want your Schema App Editor or Highlighter markup deploying on your site.

function schema_markup_disable_default ( $schema_markup, $schema_markup_type, $post, $post_type ) {  
	if ( $schema_markup_type == 'Default' ) {      
		return ''; 
	}  
    return $schema_markup;
}
add_filter( 'hunch_schema_markup', 'schema_markup_disable_default', 10, 4 );


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 );


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' );


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 at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article