Skip to content

Instantly share code, notes, and snippets.

@hchouhan
Last active September 5, 2018 12:27
Show Gist options
  • Save hchouhan/cfb55ac718a3db91365570cf566feb72 to your computer and use it in GitHub Desktop.
Save hchouhan/cfb55ac718a3db91365570cf566feb72 to your computer and use it in GitHub Desktop.
Old Widget in IRT plugin
add_action('init', array($this, 'add_widget_most_recommended_posts_old'));
//add_action('widgets_init', array($this, 'widget_most_recommended_posts'));
/*--------------------------------------------*
* Widget
*--------------------------------------------*/
function add_widget_most_recommended_posts_old() {
function most_recommended_posts($numberOf, $before, $after, $show_count, $post_type = "post", $raw = false) {
global $wpdb;
$request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
$request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
$request .= " AND post_status='publish' AND post_type='$post_type' AND meta_key='_recommended'";
$request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $numberOf";
$posts = $wpdb->get_results($request);
if ($raw):
return $posts;
else:
foreach ($posts as $item) {
$post_title = stripslashes($item->post_title);
$permalink = get_permalink($item->ID);
$post_count = $item->meta_value;
echo $before . '<a href="' . $permalink . '" title="' . $post_title . '" rel="nofollow">' . $post_title . '</a>';
echo $show_count == '1' ? ' (' . $post_count . ')' : '';
echo $after;
}
endif;
}
function widget_most_recommended_posts_old($args) {
extract($args);
$options = get_option("most_recommended_posts");
if (!is_array($options)) {
$options = array(
'title' => __('Most recommended posts', 'i-recommend-this'),
'number' => __('5', 'i-recommend-this'),
'show_count' => '0'
);
}
$title = $options['title'];
$numberOf = absint( $options['number'] );
$show_count = $options['show_count'];
echo $before_widget;
echo $before_title . $title . $after_title;
echo '<ul class="mostrecommendedposts">';
most_recommended_posts($numberOf, '<li>', '</li>', $show_count);
echo '</ul>';
echo $after_widget;
}
wp_register_sidebar_widget('most_recommended_posts', __('Most recommended posts', 'i-recommend-this'), 'widget_most_recommended_posts_old');
/* Form */
function options_widget_most_recommended_posts() {
$options = get_option("most_recommended_posts");
if (!is_array($options)) {
$options = array(
'title' => __('Most recommended posts', 'i-recommend-this'),
'number' => __('5', 'dot'),
'show_count' => '0'
);
}
?>
<p><label for="mrp-title"><?php _e('Title:', 'i-recommend-this'); ?><br/>
<input class="widefat" type="text" id="mrp-title" name="mrp-title"
value="<?php echo $options['title']; ?>"/></label></p>
<p><label for="mrp-number"><?php _e('Number of posts to show:', 'i-recommend-this'); ?><br/>
<input type="text" id="mrp-number" name="mrp-number" style="width: 25px;"
value="<?php echo $options['number']; ?>"/>
<small>(max. 15)</small>
</label></p>
<p><label for="mrp-show-count"><input type="checkbox" id="mrp-show-count" name="mrp-show-count"
value="1"<?php if ($options['show_count'] == '1') echo 'checked="checked"'; ?> /> <?php _e('Show post count', 'i-recommend-this'); ?>
</label></p>
<input type="hidden" id="mrp-submit" name="mrp-submit" value="1"/>
<?php
if (isset($_POST['mrp-submit'])) {
$options['title'] = htmlspecialchars($_POST['mrp-title']);
$options['number'] = htmlspecialchars($_POST['mrp-number']);
$options['show_count'] = $_POST['mrp-show-count'];
if ($options['number'] > 15) {
$options['number'] = 15;
}
update_option("most_recommended_posts", $options);
}
}
wp_register_widget_control('most_recommended_posts', __('Most recommended posts', 'i-recommend-this'), 'options_widget_most_recommended_posts');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment