|
/ Documentation /General/ Add an Extra Field for a Schema Type

Add an Extra Field for a Schema Type

Schema Pro allows you to add an extra field for a particular schema type.

You will need to use 2 filters to be able to add an extra field.

  1. wp_schema_pro_schema_meta_fields: This filter is used to add your extra field for mapping.
  2. wp_schema_pro_schema_{$schema_type}: This filter is used to map the extra field with schema field. Here $schema_type should be replaced with your schema type for which you wish to add an extra field. You can use the following schema types.   articlebookcourseeventjob_postinglocal_businessreviewpersonproduct,
    recipeservicesoftware_applicationvideo_object

Here is an example you can refer to:

Example: Add a field workExample in local_business Schema type. Add following code in your functions.php file. Here work-example is used as a key for the workExample field.

add_action( 'after_setup_theme', 'add_my_custom_meta_field' );
function add_my_custom_meta_field() {
	add_filter( 'wp_schema_pro_schema_meta_fields', 'my_extra_schema_field' );
	add_filter( 'wp_schema_pro_schema_local_business', 'my_extra_schema_field_mapping', 10, 3 );
}

/**
 * Add fields for mapping.
 *
 * @param  array $fields Mapping fields array.
 * @return array
 */
function my_extra_schema_field( $fields ) {
	$fields['bsf-aiosrs-local-business']['subkeys']['work-example'] = array( // `bsf-aiosrs-book` used for Book, `bsf-aiosrs-event` will for Event like that.
		'label'    => esc_html__( 'workExample', 'wp-schema-pro' ), // Label to display in Mapping fields
		'type'     => 'text', // text/date/image
		'default'  => 'none',
		'required' => true, // true/false.
	);

	return $fields;
}

/**
 * Mapping extra field for schema markup.
 *
 * @param  array $schema Schema array.
 * @param  array $data   Mapping fields array.
 * @return array
 */
function my_extra_schema_field_mapping( $schema, $data, $post ) {

	if ( isset( $data['work-example'] ) && ! empty( $data['work-example'] ) ) {
		// For date/text type field
		$schema['workExample'] = esc_html( $data['work-example'] );

		// For image type field
		// $schema['workExample'] = BSF_AIOSRS_Pro_Schema_Template::get_image_schema( $data['work-example'] );
	}
	return $schema;
}
Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
On this page
Scroll to Top