Skip to content

Instantly share code, notes, and snippets.

@ramiabraham
Created December 27, 2013 03:46
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 ramiabraham/8142319 to your computer and use it in GitHub Desktop.
Save ramiabraham/8142319 to your computer and use it in GitHub Desktop.
widgets.php
<?php
/**
* Include and setup custom widgets
*
*/
/**
* display featured posts
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Featured extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'featured-posts widget-post-list', 'description' => __( 'Display featured posts') );
parent::__construct('featured-posts', __( 'OTT Featured Posts' ), $widget_ops);
$this->alt_option_name = 'featured-posts';
}
function content( $count ) {
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'posts_per_page' => $count,
'meta_key' => '_ott_post_featured',
'meta_value' => 'on'
);
$items = get_posts( $args );
if ( ! $items )
return false;
return $items;
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$count = isset( $instance['count'] ) ? $instance['count'] : 4;
$items = $this->content( $count );
if ( empty( $items ) )
return;
echo $before_widget;
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul>';
foreach ( $items as $post_id ):
// get variables
$title = get_the_title( $post_id );
$link = get_permalink( $post_id );
$image = get_the_post_thumbnail( $post_id );
echo '<li>';
if ( !empty( $image ) )
echo '<a class="item-image" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$image.'</a>';
echo '<a class="item-title" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Featured Posts',
'count' => '4',
));
$title = strip_tags( $instance['title'] );
$count = strip_tags( $instance['count'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">Post Count:</label>
<input class="small-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
</p>
<?php }
} // end widget class
/**
* display recent posts
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Recent extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'recent-posts widget-post-list', 'description' => __( 'Display recent posts') );
parent::__construct('recent-posts', __( 'OTT Recent Posts' ), $widget_ops);
$this->alt_option_name = 'recent-posts';
}
function content( $count ) {
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'posts_per_page' => $count,
);
$items = get_posts( $args );
if ( ! $items )
return false;
return $items;
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$count = isset( $instance['count'] ) ? $instance['count'] : 4;
$items = $this->content( $count );
if ( empty( $items ) )
return;
echo $before_widget;
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul>';
foreach ( $items as $post_id ):
// get variables
$title = get_the_title( $post_id );
$link = get_permalink( $post_id );
$image = get_the_post_thumbnail( $post_id, 'side-bar-thumb' );
echo '<li>';
if ( !empty( $image ) )
echo '<a class="item-image" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$image.'</a>';
echo '<a class="item-title" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Recent Posts',
'count' => '4',
));
$title = strip_tags( $instance['title'] );
$count = strip_tags( $instance['count'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">Post Count:</label>
<input class="small-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
</p>
<?php }
} // end widget class
/**
* display highest commented posts
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Commented extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'most-commented widget-post-list', 'description' => __( 'Display most commented posts') );
parent::__construct('most-commented', __( 'OTT Commented Posts' ), $widget_ops);
$this->alt_option_name = 'most-commented';
}
function content( $count ) {
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'posts_per_page' => $count,
'orderby' => 'comment_count'
);
$items = get_posts( $args );
if ( ! $items )
return false;
return $items;
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$count = isset( $instance['count'] ) ? $instance['count'] : 4;
$items = $this->content( $count );
if ( empty( $items ) )
return;
echo $before_widget;
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul>';
foreach ( $items as $post_id ):
// get variables
$title = get_the_title( $post_id );
$link = get_permalink( $post_id );
$image = get_the_post_thumbnail( $post_id, 'side-bar-thumb' );
echo '<li>';
if ( !empty( $image ) )
echo '<a class="item-image" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$image.'</a>';
echo '<a class="item-title" href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Most Commented',
'count' => '4',
));
$title = strip_tags( $instance['title'] );
$count = strip_tags( $instance['count'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">Post Count:</label>
<input class="small-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
</p>
<?php }
} // end widget class
/**
* display Google+ badge
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_GPlus_Badge extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'google-plus-badge', 'description' => __( 'Display Google+ badge') );
parent::__construct('google-plus-badge', __( 'OTT Google+' ), $widget_ops);
$this->alt_option_name = 'google-plus-badge';
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$profile = isset( $instance['profile'] ) ? $instance['profile'] : 'https://plus.google.com/116817773084272661914';
$layout = isset( $instance['layout'] ) ? $instance['layout'] : 'landscape';
$relate = isset( $instance['relate'] ) ? $instance['relate'] : 'publisher';
$profile = str_replace( 'https:', '', $profile );
echo $before_widget;
// echo '<div class="g-page" data-href="'.$profile.'" data-layout="'.$layout.'" data-showcoverphoto="false" data-rel="'.$relate.'"></div>';
// echo '<a href="'.$profile.'?prsrc=3" rel="publisher" target="_top" style="text-decoration:none;display:inline-block;color:#333;text-align:center; font:13px/16px arial,sans-serif;white-space:nowrap;">';
echo '<a class="badge-link" href="'.$profile.'?prsrc=3" rel="publisher" target="_top">';
echo '<span class="badge-name">Online Tech Tips</span> <span class="badge-split">on</span> ';
echo '<img src="//ssl.gstatic.com/images/icons/gplus-32.png" alt="Google+"/>';
echo '</a>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['profile'] = strip_tags( $new_instance['profile'] );
$instance['layout'] = strip_tags( $new_instance['layout'] );
$instance['relate'] = strip_tags( $new_instance['relate'] );
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'profile' => 'https://plus.google.com/116817773084272661914',
'layout' => 'landscape',
'relate' => 'publisher',
));
$profile = strip_tags( $instance['profile'] );
$layout = strip_tags( $instance['layout'] );
$relate = strip_tags( $instance['relate'] );
?>
<p>
<label for="<?php echo $this->get_field_id('profile'); ?>">Profile URL:</label>
<input class="widefat" id="<?php echo $this->get_field_id('profile'); ?>" name="<?php echo $this->get_field_name('profile'); ?>" type="text" value="<?php echo $profile; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('layout'); ?>">Layout:</label>
<input class="widefat" id="<?php echo $this->get_field_id('layout'); ?>" name="<?php echo $this->get_field_name('layout'); ?>" type="text" value="<?php echo esc_attr( $layout ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('relate'); ?>">Rel Attribute:</label>
<input class="widefat" id="<?php echo $this->get_field_id('relate'); ?>" name="<?php echo $this->get_field_name('relate'); ?>" type="text" value="<?php echo esc_attr( $relate ); ?>" />
</p>
<?php }
} // end widget class
/**
* display bio block
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Bio_Block extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'bio-block', 'description' => __( 'Display short bio and image in footer' ) );
parent::__construct('bio-block', __( 'OTT Bio Block' ), $widget_ops);
$this->alt_option_name = 'bio-block';
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$user_id = isset( $instance['user'] ) ? $instance['user'] : 1;
echo $before_widget;
echo get_avatar( $user_id, 88 );
$title = apply_filters( 'widget_title', get_the_author_meta( 'display_name', $user_id ) );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo wpautop( get_the_author_meta( 'description', $user_id ) );
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['user'] = absint( $new_instance['user'] );
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'user' => 1,
));
$user = absint( $instance['user'] );
?>
<p>
<label for="<?php echo $this->get_field_id('user'); ?>">User:</label>
<select class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>">
<?php
$users = get_users();
foreach ( $users as $u ):
echo '<option value="'.$u->ID.'" '.selected( $user, $u->ID, false ).'>'.$u->display_name.'</option>';
endforeach;
?>
</select>
</p>
<?php }
} // end widget class
/**
* display about site
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_About_Block extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'about-block', 'description' => __( 'Display about site info and link') );
parent::__construct('about-block', __( 'OTT About Block' ), $widget_ops);
$this->alt_option_name = 'about-block';
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
if ( !empty( $instance['about'] ) )
echo wpautop( $instance['about'] );
if ( !empty( $instance['page'] ) )
echo '<a class="read-more" rel="author" href="'.get_bloginfo( 'url' ).'/about">Read More</a>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['about'] = $new_instance['about'];
$instance['page'] = absint( $new_instance['page'] );
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'About Online Tech Tips',
'about' => '',
'page' => ''
));
$title = strip_tags( $instance['title'] );
$about = $instance['about'];
$page = absint( $instance['page'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('about'); ?>">About Text:</label>
<textarea class="widefat" cols="20" rows="8" id="<?php echo $this->get_field_id('about'); ?>" name="<?php echo $this->get_field_name('about'); ?>"><?php echo esc_attr( $about ); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('page'); ?>">Linked Page:</label>
<select class="widefat" id="<?php echo $this->get_field_id('page'); ?>" name="<?php echo $this->get_field_name('page'); ?>">
<?php
$args = array(
'fields' => 'ids',
'post_type' => 'page',
'nopaging' => true
);
$pages = get_posts( $args );
foreach ( $pages as $p ):
echo '<option value="'.$p.'" '.selected( $page, $p, false ).'>'.get_the_title( $p ).'</option>';
endforeach;
?>
</select>
</p>
<?php }
} // end widget class
/**
* display sidebar ad
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Adsense_Block extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'adsense-block', 'description' => __( 'Display AdSense ad') );
parent::__construct('adsense-block', __( 'OTT Ad Block' ), $widget_ops);
$this->alt_option_name = 'adsense-block';
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
if ( !isset( $instance['code'] ) || empty( $instance['code'] ) )
return;
echo $before_widget;
echo $instance['code'];
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['code'] = $new_instance['code'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'code' => ''
));
$code = $instance['code'];
?>
<p>
<label for="<?php echo $this->get_field_id('code'); ?>">Ad Code:</label>
<textarea class="widefat" cols="20" rows="8" id="<?php echo $this->get_field_id('code'); ?>" name="<?php echo $this->get_field_name('code'); ?>"><?php echo esc_attr( $code ); ?></textarea>
</p>
<?php }
} // end widget class
/**
* display newsletter and social links
*
* @return OTT_Widgets
*
* @since 1.0
*/
class ott_Social_Signup extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'social-signup', 'description' => __( 'Display newsletter and social links') );
parent::__construct('social-signup', __( 'OTT Social Signup' ), $widget_ops);
$this->alt_option_name = 'social-signup';
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
// signup form
echo '<div id="mc_embed_signup" class="sidebar-signup-form">';
echo '<form action="http://online-tech-tips.us7.list-manage.com/subscribe/post?u=cea4775e8e99a3e05dd31829e&amp;id=15faf51d16" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>';
echo '<div class="mc-field-group">';
echo '<input class="sideform-email" type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Enter your email and hit enter">';
echo '<input class="sideform-submit" type="submit" value="&#9658;" name="subscribe" id="mc-embedded-subscribe">';
echo '</div>';
echo '<div id="mce-responses" class="clear">';
echo '<div class="response" id="mce-error-response" style="display:none"></div>';
echo '<div class="response" id="mce-success-response" style="display:none"></div>';
echo '</div>';
echo '</form>';
echo '</div>';
// social links
$header = empty( $instance['header'] ) ? '' : apply_filters( 'widget_title', $instance['header'] );
if ( !empty( $header ) ) { echo $before_title . $header . $after_title; };
echo '<div class="social-links">';
if ( !empty( $instance['email'] ) )
echo '<span class="social-icon"><a rel="nofollow" class="icon-envelope" href="mailto:'.antispambot( $instance['email'] ).'"></a></span>';
if ( !empty( $instance['gplus'] ) )
echo '<span class="social-icon"><a rel="nofollow" class="icon-google-plus" href="'.esc_url( $instance['gplus'] ).'"></a></span>';
if ( !empty( $instance['faceb'] ) )
echo '<span class="social-icon"><a rel="nofollow" class="icon-facebook" href="'.esc_url( $instance['faceb'] ).'"></a></span>';
if ( !empty( $instance['twit'] ) )
echo '<span class="social-icon"><a rel="nofollow" class="icon-twitter" href="'.esc_url( $instance['twit'] ).'"></a></span>';
if ( !empty( $instance['feed'] ) )
echo '<span class="social-icon"><a rel="nofollow" class="icon-rss" href="'.esc_url( $instance['feed'] ).'"></a></span>';
echo '</div>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['header'] = strip_tags( $new_instance['header'] );
$instance['email'] = strip_tags( $new_instance['email'] );
$instance['gplus'] = strip_tags( $new_instance['gplus'] );
$instance['faceb'] = strip_tags( $new_instance['faceb'] );
$instance['twit'] = strip_tags( $new_instance['twit'] );
$instance['feed'] = strip_tags( $new_instance['feed'] );
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => 'Daily Email Newsletter',
'header' => '',
'email' => 'akishore@online-tech-tips.com',
'gplus' => 'https://plus.google.com/116817773084272661914/post',
'faceb' => 'https://www.facebook.com/pages/Online-Tech-Tips/94959006140',
'twit' => 'http://twitter.com/akishore',
'feed' => get_bloginfo( 'rss2_url' )
));
$title = strip_tags( $instance['title'] );
$header = strip_tags( $instance['header'] );
$email = strip_tags( $instance['email'] );
$gplus = strip_tags( $instance['gplus'] );
$faceb = strip_tags( $instance['faceb'] );
$twit = strip_tags( $instance['twit'] );
$feed = strip_tags( $instance['feed'] );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('header'); ?>">Social Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('header'); ?>" name="<?php echo $this->get_field_name('header'); ?>" type="text" value="<?php echo esc_attr($header); ?>" />
<span class="description">Optional title for social icons</span>
</p>
<p>
<label for="<?php echo $this->get_field_id('email'); ?>">Email Address:</label>
<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="email" value="<?php echo $email; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('gplus'); ?>">Google+:</label>
<input class="widefat" id="<?php echo $this->get_field_id('gplus'); ?>" name="<?php echo $this->get_field_name('gplus'); ?>" type="url" value="<?php echo esc_url( $gplus ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('faceb'); ?>">Facebook:</label>
<input class="widefat" id="<?php echo $this->get_field_id('faceb'); ?>" name="<?php echo $this->get_field_name('faceb'); ?>" type="url" value="<?php echo esc_url( $faceb ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('twit'); ?>">Twitter:</label>
<input class="widefat" id="<?php echo $this->get_field_id('twit'); ?>" name="<?php echo $this->get_field_name('twit'); ?>" type="url" value="<?php echo esc_url( $twit ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('feed'); ?>">RSS Feed:</label>
<input class="widefat" id="<?php echo $this->get_field_id('feed'); ?>" name="<?php echo $this->get_field_name('feed'); ?>" type="url" value="<?php echo esc_url( $feed ); ?>" />
</p>
<?php }
} // end widget class
/**
* call all our widgets
*
* @return OTT_Widgets
*
* @since 1.0
*/
add_action( 'widgets_init', create_function( '', "register_widget('ott_Social_Signup');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_Featured');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_Recent');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_Commented');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_GPlus_Badge');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_Bio_Block');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_About_Block');" ) );
add_action( 'widgets_init', create_function( '', "register_widget('ott_Adsense_Block');" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment