Benoit, a developer and very welcoming customer of mine, asked how he can integrate comments into this schemas. Here is an example field type that I’ve coded for him. Feel free to customize it the way you like it.
I’ve already created a full post on how to create your own field type in SNIP so that you can build nearly anything that fits your needs.
Based on the example on the above linked page I create another example that allows you to create comments into schemas.
And here it is:
- Download it from Github (as ZIP file).
- Upload it in your WordPress dashboard.
- Activate the plugin.
- You now have a new field “Comments” active.
<?php
/*
Plugin Name: SNIP Comments Field
Description: Allows to integrate comments into Structured Data.
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_comf_old_php_notice' );
function snip_comf_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-comf' ),
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_comf_subselects' );
/**
* Adds new field to use in Global Snippets in the SNIP plugin.
*
* @param array $values
*
* @return array
* @since 0.1.0
*
*/
function snip_comf_subselects( $values ) {
$values['http://schema.org/Comment'][] = [
'id' => 'snip_comf_comments',
'label' => esc_html_x( 'List of comments', 'subselect field', 'snip-comf' ),
'method' => 'snip_comf_comments_callback',
];
return $values;
}
/**
* Returns the value.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @return stdClass[]
*
* @since 0.1.0
*/
function snip_comf_comments_callback( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$comments = [];
global $wp_query;
if ( ! is_array( $wp_query->comments ) ) {
return $comments;
}
/**
* @var WP_Comment $comment
*/
foreach ( $wp_query->comments as $comment ) {
$c = new stdClass();
$c->{'@type'} = 'Comment';
$c->{'@context'} = 'http://schema.org';
$c->dateCreated = date_i18n( 'c', $comment->comment_date );
$c->text = esc_html( $comment->comment_content );
$c->author = new stdClass();
$c->author->{'@type'} = 'Person';
$c->author->{'@context'} = 'http://schema.org';
$c->author->name = $comment->comment_author;
$comments[] = $c;
}
return $comments;
}