Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Created September 29, 2015 21:15
Show Gist options
  • Select an option

  • Save designbuildtest/aea3d135eb5294d66647 to your computer and use it in GitHub Desktop.

Select an option

Save designbuildtest/aea3d135eb5294d66647 to your computer and use it in GitHub Desktop.
Testimonials with ratings
/**
* Create a Rating metabox for Accolades.
*/
function dbt_accolade_rating_metabox() {
add_meta_box( 'dbt_accolade_rating_metabox', __( 'Testimonial Rating' ), 'dbt_accolade_rating_callback', 'accolade', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'dbt_accolade_rating_metabox' );
function dbt_accolade_rating_callback( $post ) {
wp_nonce_field( 'accolade_rating_check', 'accolade_rating_nonce' );
$current_value = get_post_meta( $post->ID, '_dbt_accolade_rating', true );
?>
<select id="accolade_rating" name="accolade_rating">
<?php
$options = array(
'5' => __( '5 Stars' ),
'4' => __( '4 Stars' ),
'3' => __( '3 Stars' ),
'2' => __( '2 Stars' ),
'1' => __( '1 Star' ),
);
foreach( $options as $key => $value ) {
if ( $key == $current_value ) { ?>
<option value="<?php esc_attr_e( $key ); ?>" selected><?php esc_attr_e( $value ); ?></option><?php }
else { ?>
<option value="<?php esc_attr_e( $key ); ?>"><?php esc_attr_e( $value ); ?></option><?php
}
}
?>
</select>
<?php
}
function dbt_save_accolade_rating( $post_id ) {
if ( ! isset( $_POST['accolade_rating_nonce'] ) ) { return; }
if ( ! wp_verify_nonce( $_POST['accolade_rating_nonce'], 'accolade_rating_check' ) ) { return; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
if ( ! current_user_can( 'edit_page', $post_id ) ) { return; }
if ( isset( $_POST['accolade_rating'] ) ) {
$value = $_POST['accolade_rating'];
$valid = array( '1', '2', '3', '4', '5' );
if ( in_array( $value, $valid ) ) {
update_post_meta( $post_id, '_dbt_accolade_rating', $value );
}
}
}
add_action( 'save_post', 'dbt_save_accolade_rating' );
/**
* List Testimonials (Text Accolades).
*/
function dbt_text_accolades_html( $number, $class, $imagesize ) {
$args = array(
'post_type' => 'accolade',
'post_status' => 'publish',
'posts_per_page' => absint( $number ),
'meta_key' => '_dbt_accolade_type',
'meta_value' => 'testimonial',
'meta_compare' => '===',
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true
);
$text_accolades = new WP_Query( $args );
if ( $text_accolades->have_posts() ) { ?>
<ul itemscope itemtype="http://schema.org/LocalBusiness" class="accolades <?php esc_attr_e( $class ); ?>">
<meta itemprop="name" content="<?php bloginfo( 'name' ); ?>">
<meta itemprop="description" content="<?php bloginfo( 'description' ); ?>">
<?php $rating_total = ''; ?>
<?php $review_count = ''; ?>
<?php while ( $text_accolades->have_posts() ) : $text_accolades->the_post(); ?>
<li itemprop="review" itemscope itemtype="http://schema.org/Review" <?php post_class(); ?>>
<?php if ( $imagesize !== 'none' ) { the_post_thumbnail( esc_attr( $imagesize ) ); } ?>
<blockquote itemprop="description">
<?php the_content(); ?>
<?php
$link = get_post_meta( get_the_ID(), '_dbt_accolade_link', true );
if ( $link ) { ?>
<a href="<?php echo esc_url( $link ); ?>"><?php the_title( '<cite itemprop="author">', '</cite>' ); ?></a><?php
}
else {
the_title( '<cite itemprop="author">', '</cite>' );
}
?>
<meta itemprop="datePublished" content="<?php the_time( get_option( 'date_format' ) ); ?>">
<span itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
<?php $rating_value = absint( get_post_meta( get_the_ID(), '_dbt_accolade_rating', true ) ); ?>
<meta itemprop="ratingValue" content="<?php echo $rating_value; ?>">
</span>
</blockquote>
</li>
<?php $rating_total = $rating_total + $rating_value; ?>
<?php $review_count++; ?>
<?php endwhile; ?>
<?php
if ( $review_count > 1 ) { ?>
<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="ratingValue" content="<?php echo ( $rating_total / $review_count ); ?>"><meta itemprop="reviewCount" content="<?php echo $review_count; ?>">
</span><?php
}
?>
<?php wp_reset_postdata(); ?>
</ul><?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment