Skip to content

Instantly share code, notes, and snippets.

@code-flow
Created August 22, 2019 14:26
Show Gist options
  • Save code-flow/e3212c810eeb9b9b28a0c5b17e786a1f to your computer and use it in GitHub Desktop.
Save code-flow/e3212c810eeb9b9b28a0c5b17e786a1f to your computer and use it in GitHub Desktop.
SNIP Basic Field Type Example
<?php
/*
Plugin Name: SNIP Basic Field Type Example
Description: A plugin that adds a new Field Type in SNIP.
Author: Florian Simeth
Version: 0.1.0
Author URI: https://rich-snippets.io
Plugin URI: https://rich-snippets.io/how-to-add-your-own-field-type/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
/**
*
* PHP Version check.
*
*/
if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
add_action( 'admin_notices', 'snip_fte_old_php_notice' );
function snip_fte_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 Field Type Example plugin. Thank you!', 'snip-fte' ),
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_fte_subselects' );
/**
* Adds new field to use in Global Snippets in the SNIP plugin.
*
* @param array $values
*
* @return array
* @since 0.1.0
*
*/
function snip_fte_subselects( $values ) {
$values['http://schema.org/Number'][] = [
'id' => 'snip_fte_rating_value',
'label' => esc_html_x( 'My Custom rating value', 'subselect field', 'snip-fte' ),
'method' => 'snip_fte_rating_value_callback',
];
$values['http://schema.org/Integer'][] = [
'id' => 'snip_fte_rating_count',
'label' => esc_html_x( 'My Custom rating count', 'subselect field', 'snip-fte' ),
'method' => 'snip_fte_rating_count_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_fte_rating_value_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
/**
* Load and/or calculate something here.
*/
$value = 5;
return $value;
}
/**
* Returns the count.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @return int
*/
function snip_fte_rating_count_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
/**
* Load and/or calculate something here.
*/
$count = 100;
return $count;
}
@code-flow
Copy link
Author

This is an example WordPress plugin to create your own fields in SNIP, my Structured Data Plugin for WordPress. Learn more about the code above in the FAQ: How to add your own field type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment