Skip to content

Instantly share code, notes, and snippets.

@code-flow
Last active September 30, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-flow/b0c48ec2426cac21d487202e6a5a2cee to your computer and use it in GitHub Desktop.
Save code-flow/b0c48ec2426cac21d487202e6a5a2cee to your computer and use it in GitHub Desktop.
SNIP Comments Field
<?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/comments-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;
}
@code-flow
Copy link
Author

This example shows you you can integrate an array of simple comments into Structured Data generated with SNIP - The Structured Data Plugin for WordPress.
Read the full how to here: https://rich-snippets.io/comments-field-type/

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