Guest User

Untitled

a guest
Jan 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");}
  2.  
  3. <?php
  4. $prefix = 'pk_rs_';
  5.  
  6. DEFINE ('PK_RS_DEFAULT_RATING', '-');
  7.  
  8. DEFINE ('PK_RS_DISPLAY', true);
  9.  
  10. $pk_rs_meta_box = array(
  11. 'id' => 'pk_rich_snippet_review',
  12. 'title' => 'Google Rich Snippets: Reviews',
  13. 'page' => 'post',
  14. 'context' => 'normal',
  15. 'priority' => 'high',
  16. 'fields' => array(
  17. array(
  18. 'name' => 'Rating',
  19. 'desc' => 'Product rating, from 1 to 5.',
  20. 'id' => 'rating',
  21. 'type' => 'select',
  22. 'options' => array('-', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0')
  23. ),
  24. array(
  25. 'name' => 'Author',
  26. 'desc' => 'Author display name.',
  27. 'id' => 'author',
  28. 'type' => 'text',
  29. 'std' => ''
  30. )
  31.  
  32. )
  33. );
  34.  
  35. add_action('admin_menu', 'pk_rich_snippet_add_box');
  36.  
  37. // Add meta box
  38. function pk_rich_snippet_add_box() {
  39. global $pk_rs_meta_box;
  40.  
  41. add_meta_box($pk_rs_meta_box['id'], $pk_rs_meta_box['title'], 'pk_rich_snippet_show_box', $pk_rs_meta_box['page'], $pk_rs_meta_box['context'], $pk_rs_meta_box['priority']);
  42. }
  43.  
  44. // Callback function to show fields in meta box
  45. function pk_rich_snippet_show_box() {
  46. global $pk_rs_meta_box, $post;
  47.  
  48. // Use nonce for verification
  49. echo '<input type="hidden" name="pk_rich_snippet_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  50.  
  51. echo '<table class="form-table">';
  52.  
  53. foreach ($pk_rs_meta_box['fields'] as $field) {
  54. // get current post meta data
  55. $meta = get_post_meta($post->ID, $field['id'], true);
  56.  
  57. echo '<tr>',
  58. '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  59. '<td>';
  60. switch ($field['type']) {
  61. case 'text':
  62. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  63. '<br />', $field['desc'];
  64. break;
  65. case 'textarea':
  66. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  67. '<br />', $field['desc'];
  68. break;
  69. case 'select':
  70. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  71. foreach ($field['options'] as $option) {
  72. echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  73. }
  74. echo '</select>', '<br />', $field['desc'];
  75. break;
  76. case 'radio':
  77. foreach ($field['options'] as $option) {
  78. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  79. }
  80. break;
  81. case 'checkbox':
  82. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />',
  83. '<br />', $field['desc'];
  84. break;
  85. }
  86. echo '<td>',
  87. '</tr>';
  88. }
  89.  
  90. echo '</table>';
  91. }
  92.  
  93. add_action('save_post', 'pk_rs_save_data');
  94.  
  95. // Save data from meta box
  96. function pk_rs_save_data($post_id) {
  97. global $pk_rs_meta_box;
  98.  
  99. // verify nonce
  100. if (!wp_verify_nonce($_POST['pk_rich_snippet_nonce'], basename(__FILE__))) {
  101. return $post_id;
  102. }
  103.  
  104. // check autosave
  105. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  106. return $post_id;
  107. }
  108.  
  109. // check permissions
  110. if ('page' == $_POST['post_type']) {
  111. if (!current_user_can('edit_page', $post_id)) {
  112. return $post_id;
  113. }
  114. } elseif (!current_user_can('edit_post', $post_id)) {
  115. return $post_id;
  116. }
  117.  
  118. foreach ($pk_rs_meta_box['fields'] as $field) {
  119. $old = get_post_meta($post_id, $field['id'], true);
  120. $new = $_POST[$field['id']];
  121.  
  122. if ($new && $new != $old) {
  123. update_post_meta($post_id, $field['id'], $new);
  124. } elseif ('' == $new && $old) {
  125. delete_post_meta($post_id, $field['id'], $old);
  126. }
  127. }
  128. }
  129.  
  130. add_filter('the_content', 'pk_rs_add_rich_snippet_to_content', 20);
  131. function pk_rs_add_rich_snippet_to_content($content){
  132. if (is_single()&&!is_feed()){
  133. global $post;
  134. $rating = get_post_meta($post->ID, 'rating', true);
  135. $rating = ( '-' == $rating && '-' != PK_RS_DEFAULT_RATING ) ? PK_RS_DEFAULT_RATING : $rating;
  136. if ( '-' != $rating ){
  137. $title = $post->post_title ;
  138. $dateTime = date_create( $post->post_date );
  139. $date = $dateTime->format("Y-m-d");
  140. $date_only = $dateTime->format("M j");
  141. $author = get_post_meta($post->ID, 'author', true);
  142. $author = ( '' == $author ) ? ucfirst(get_the_author_meta('display_name', $post->post_author)) : $author;
  143. $summary = get_post_meta($post->ID, 'summary', true);
  144. $description = get_post_meta($post->ID, 'description', true);
  145. if ( !PK_RS_DISPLAY ) {
  146. $output = "<div class="hreview" style="display:none">";
  147. $output .= "<span class="item"><span class="fn entry-title">".$title."</span></span>";
  148. $output .= "Reviewed by <span class="reviewer">".$author."</span> on <span class="dtreviewed"> ".$date_only."<span class="value-title" title="".$date.""></span></span>";
  149. $output .= "Rating: <span class="rating">".$rating."</span>";
  150. $output .= "<span class="summary">".$summary."</span>";
  151. $output .= "<span class="description">".$description."</span>";
  152. $output .= "</div>";
  153. } else {
  154. $output = "<div class="hreview" style="display:block; margin: 0 0 10px 10px; padding: 10px; background: #F6F6F6; border: 1px solid #DDD; -moz-border-radius: 3px; border-radius: 3px; font-size: 0.8em; width: 30%; float: right;">";
  155. $output .= "Title: <span class="item"><span class="fn entry-title" style="font-size: 0.8em;font-weight: normal;">".$title."</span></span><br />";
  156. $output .= "Reviewed by <span class="reviewer">".$author."</span> on <span class="dtreviewed"> ".$date_only."<span class="value-title" title="".$date.""></span></span><br/>";
  157. $output .= "Rating: <span class="rating">".$rating."</span><br/>";
  158. $output .= ( 0 < strlen($summary) ) ? "Summary: <span class="summary">".$summary."</span><br/>" : "";
  159. $output .= ( 0 < strlen($description) ) ? "<p><span class="description">".$description."</span></p>" : "";
  160. $output .= "</div>";
  161. }
  162.  
  163. $content = $output.$content ;
  164. }
  165. }
  166. return $content;
  167. }
  168. ?>
  169.  
  170. if ($rating == "5.0"){
  171. print '<img src="stars/5-0.png">';
  172. }
Add Comment
Please, Sign In to add comment