Use content from a shortcode inside of a schema property

A customer asked if it’s possible to add content from a shortcode inside of a schema property. The short answer is: SNIP does not support this at the moment, but I can see that there are use cases where this can be beneficial.

Should I use shortcode-content?

I would say no, because shortcodes are not the right way to add content to snippets. The reason for this is that shortcodes do output HTML code. However schema.org syntax does (in most cases) not allow HTML code. That could potentially lead to problems. Particularly when your structured data has a wrong format and search engines cannot crawl/index/recognize it the way it should be.

To sum it up: shortcodes are a nice function to output data in the frontend of a WordPress-site, but it’s not for Structured Data.

Can I use it anyway?

Sure. You can do everything you want to 😉 I even have an example below that you can use at your own risk:

What should I do instead?

I always tell my customers to use their own field types. Some coding is involved but it makes sure that the data that is outputted is 100% correct and crawlable, too.

Example

You can download the example below and use it as a WordPress plugin at your own risk:

<?php
/*
Plugin Name: SNIP Shortcode Example
Description: A plugin that allows to add shortcode-content to schema properties.
Author: Florian Simeth
Version: 0.1.0
Author URI: https://rich-snippets.io
Plugin URI: https://rich-snippets.io/shortcode-content-in-properties/
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

/**
 *
 * PHP Version check.
 *
 */
if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
	add_action( 'admin_notices', 'snip_scc_old_php_notice' );

	function snip_scc_old_php_notice() {

		printf(
			'<div class="notice error"><p>%s</p></div>',
			sprintf(
				__( 'Hey mate! Sorry for interrupting you. It seem\'s that you\'re using an old PHP version (your current version is %s). You should upgrade to at least 7.0 or higher in order to use the SNIP Shortcode plugin. Thank you!', 'snip-scc' ),
				esc_html( PHP_VERSION )
			)
		);
	}

	# sorry. The plugin will not work with an old PHP version.
	return;
}


add_filter( 'wpbuddy/rich_snippets/fields/internal_subselect/values', 'snip_scc_subselects' );

/**
 * Adds new field to use in Global Snippets in the SNIP plugin.
 *
 * @param array $values
 *
 * @return array
 * @since 0.1.0
 *
 */
function snip_scc_subselects( $values ) {

	$values['http://schema.org/Text'][] = [
		'id'     => 'textfield_snip_scc_shortcode',
		'label'  => esc_html_x( 'Shortcode content', 'subselect field', 'snip-scc' ),
		'method' => 'snip_scc_callback',
	];

	return $values;
}


/**
 * Returns the value.
 *
 * @param                                     $val
 * @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
 * @param array $meta_info
 *
 * @return int|float
 */
function snip_scc_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
	$shortcode_content = do_shortcode( $val );

	return strip_tags( $shortcode_content );
}

Here is a link to the Github gist.