Hello!
I have this Twitter widget that I am using on a few themes. Currently, if you drag it to a sidebar, you can only use it once. I have a theme that could use it in a few spots. Is there a way to make this a multi-use widget like some of the default WP widgets? My widget code is below.
<?php
wp_register_sidebar_widget('ok_twitter','PrePress Twitter Widget','show_ok_twitter');
register_widget_control('ok_twitter','twitter_control');
function twitter_control(){
echo "Title:<input type='text' name='dp-twitter-title' value='" .esc_attr(get_option('ok_twitter_title')). "'/><br/></br>";
if(isset($_POST['dp-twitter-title'])) update_option('ok_twitter_title',$_POST['dp-twitter-title']);
echo "Username:<input type='text' name='dp-twitter-user' value='" .esc_attr(get_option('ok_twitter_user')). "'/><br/></br>";
if(isset($_POST['dp-twitter-user'])) update_option('ok_twitter_user',$_POST['dp-twitter-user']);
echo "Tweet count:<input type='text' name='dp-twitter-count' value='" .esc_attr(get_option('ok_twitter_count')). "'/>";
if(isset($_POST['dp-twitter-count'])) update_option('ok_twitter_count',$_POST['dp-twitter-count']);
}
function show_ok_twitter(){ ?>
<div class="holder">
<div class="footer_twitter">
<a class="moretweets" href="http://twitter.com/<?php echo get_option('ok_twitter_user', true) ?>">Follow Me +</a>
<h2><?php echo stripslashes(get_option('ok_twitter_title')) ?></h2>
<div style="clear:both;"></div>
<?php $twitter = get_option('ok_twitter_user');?>
<?php $count = get_option('ok_twitter_count');?>
<em><?php wp_echoTwitter($twitter, $count); ?></em>
</div>
</div>
<?php }
//Display Twitter Code
function wp_echoTwitter($username, $numb=3){
include_once(ABSPATH.WPINC.'/rss.php');
$twittercount = get_option('ok_twitter_count');
$numb = (is_null($twittercount)) ? $numb : $twittercount;
$tweet = fetch_rss("http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $numb );
for($i=0; $i<$numb; $i++){
echo "<li>" . html_entity_decode( $tweet->items[$i]['atom_content'] ) . "</li>";
}
}
?>
Any help is appreciated!
Mike
Denzel Chia answers:
Hi,
Please copy the below codes and overwrite your widget codes.
Go to widgets in WordPress Admin, and find the same Widget name,
PrePress Twitter Widget, and you will be able to drag and drop as many as you like.
set it up and view it working on your sidebar.
<?php
add_action( 'widgets_init', 'load_prepress_twitter_widget' );
function load_prepress_twitter_widget() {
register_widget( 'PrePressTwitterWidget' );
}
class PrePressTwitterWidget extends WP_Widget {
function PrePressTwitterWidget() {
$widget_ops = array( 'classname' => 'prepress-twitter', 'description' => __('PrePress Twitter Widget', 'prepress-twitter') );
$control_ops = array( 'width' => 200, 'height' => 350, 'id_base' => 'prepress-twitter' );
$this->WP_Widget( 'prepress-twitter', __('PrePress Twitter Widget', 'prepress-twitter'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = $instance['title'];
$username = $instance['username'];
$tweetcount= $instance['tweetcount'];
echo $before_widget;
?>
<div class="holder">
<div class="footer_twitter">
<a class="moretweets" href="http://twitter.com/<?php echo $username; ?>">Follow Me +</a>
<h2><?php if($title){echo $title;}?></h2>
<div style="clear:both;"></div>
<?php print_prepress_twitter($username, $tweetcount); ?>
</div>
</div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['username'] = $new_instance['username'];
$instance['tweetcount'] = $new_instance['tweetcount'];
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'username' => '', 'tweetcount' => '') );
$instance['title'] = $instance['title'];
$instance['username'] = $instance['username'];
$instance['tweetcount'] = $instance['tweetcount'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" /></label></p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>">Username:
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo $instance['username']; ?>" /></label></p>
<p>
<label for="<?php echo $this->get_field_id('tweetcount'); ?>">Tweet count:
<input class="widefat" id="<?php echo $this->get_field_id('tweetcount'); ?>" name="<?php echo $this->get_field_name('tweetcount'); ?>" type="text" value="<?php echo $instance['tweetcount']; ?>" /></label></p>
<p>
<?php
}
}
function print_prepress_twitter($username, $numb){
include_once(ABSPATH.WPINC.'/rss.php');
$tweet = fetch_rss("http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $numb );
for($i=0; $i<$numb; $i++){
echo "<li>" . html_entity_decode( $tweet->items[$i]['atom_content'] ) . "</li>";
}
}
?>
I will send you a copy of this codes, in case those posted here gets corrupted.
And also Please find the attached screenshot as prove of code working on my localhost.
Thanks.
Denzel
Mike McAlister comments:
Looks good, Denzel! Thanks!