Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment