Skip to content

Instantly share code, notes, and snippets.

@1naveengiri
Last active April 13, 2018 18:05
Show Gist options
  • Save 1naveengiri/8cb80e378c4d3e69a5854b33d9ff6939 to your computer and use it in GitHub Desktop.
Save 1naveengiri/8cb80e378c4d3e69a5854b33d9ff6939 to your computer and use it in GitHub Desktop.
Top/Popular post by Google analytics in WordPress
<?php
/**
* This widget is depend on https://wordpress.org/plugins/google-analytics-post-pageviews/
*
* Steps:
* 1. Create a Google developer project - http://console.developers.google.com/
*
* 2.enable google analytics service, you can check
* - https://www.youtube.com/watch?v=AX9PtgKPnuM,
* - https://developers.google.com/ad-exchange/rtb/open-bidder/google-app-guide
*
* 3. Get Client ID and secret key
*
* 4. activate and setup https://wordpress.org/plugins/google-analytics-post-pageviews/
*
* 5. check widget section for Top Post Widget
*/
/**
* The base widget for Showing Top post by Google analytics
*
*/
class TopPostWidget extends WP_Widget {
private $popPosts;
/**
* Sets up the widget
*
* @since 0.1
*/
public function __construct() {
parent::__construct(
'gd-analytic-top-posts', // Base ID
__( 'Top Posts', 'largo' ), // Name
array( 'description' => __( 'List Top posts', 'largo' ), ) // Args
);
}
/**
* Output the widget
*
*
* @param array $args
* @param array $instance
*/
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent ' . $posts_term, 'largo') : $instance['title'], $instance );
/*
* Start drawing the widget
*/
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
$args = array(
'post_type' => 'post',
'orderby' => 'meta_value_num',
'meta_key' => '_gapp_post_views',
);
$num_posts = ( isset( $instance['num_posts'] ) && !empty( $instance['num_posts'] ) )? $instance['num_posts']: 5;
if( isset( $num_posts ) && !empty( $num_posts ) && $num_posts > 1 ){
$args['posts_per_page'] = $num_posts;
}
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ){
echo '<div class="top-post-container">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="gd-toppost-item">';
echo '<div class="left">';
if (has_post_thumbnail( ) ){
echo get_the_post_thumbnail( get_the_ID(), array( 160, 100 ) );
} else{
$image = 'http://defaultimage...com';
echo '<img src="' . $image . '" width="160" height="100" >';
}
echo '</div>';
echo '<div class="right">';
$top_post_categories = get_the_category( );
if( !empty( $top_post_categories ) ){
echo '<ul>';
foreach( $top_post_categories as $top_post_category ) {
if( isset( $top_post_category->name )){
$category_link = get_category_link( $top_post_category->term_id );
echo '<li><a href="' . $category_link . '">' . $top_post_category->name . '</a></li>';
}
}
echo '</ul>';
}
echo '<a href="'. get_permalink().'">'. get_the_title() .'</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
}
wp_reset_postdata();
echo $after_widget;
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
$defaults = array(
'title' => __('popular', 'largo'),
'num_posts' => 5,
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'largo' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e( 'Number of posts to show:', 'largo' ); ?></label>
<input id="<?php echo $this->get_field_id( 'num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" value="<?php echo $instance['num_posts']; ?>" style="width:90%;" type="number"/>
</p>
<?php
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['num_posts'] = intval( $new_instance['num_posts'] );
return $instance;
}
}
add_action( 'widgets_init', function(){
register_widget( 'TopPostWidget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment