I have a few books I've been following on this. I made my own custom WP theme, which works fine, however I decided I wanted to make the right sidebar a widget area, and turn my twitter feed into a widget instead of hardcoding it into the template. I understand there are tons of twitter feed plugins out there, however I am doing this for the experience.
Plugin file:
class sp_twitterWidget extends WP_Widget
{
function sp_twitterWidget()
{
parent::WP_Widget(false, $name = 'Custom Twitter Feed');
return;
}
function widget($args, $instance)
{
extract($args);
echo $before_widget;
echo $before_title;
echo $instance['title'];
echo $after_title;
$api_url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=';
$twitter_data = file_get_contents($api_url);
$twitter_data = json_decode($twitter_data);
for ($i=1;$i<=3;$i++):
echo '<p class="tweet">';
echo $twitter_data[$i]->text;
echo '<br /><span>';
echo date("F j", strtotime($twitter_data[$i]->created_at));
echo '</span></p>';
endfor;
echo $after_widget;
}
function update($new_instance, $old_instance)
{
return $new_instance;
}
function form($instance)
{
$theTitle = esc_attr($instance['title']);
echo '<p>';
echo '<label for="'.$this->get_field_id('title').'">
Title: <input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$theTitle.'" />
</label>';
echo '</p>';
}
}
add_action('widgets_init', create_function('', 'return register_widget("sp_twitterWidget");'));
Registering sidebar as a widget-area:
if (!function_exists('register_sidebar')) { register_sidebar(); }
function sp_rightSidebar()
{
register_sidebar(array(
'name' => 'Right Sidebar',
'id' => 'rightColumn',
'description' => __('The widget area for the right sidebar', 'simplePortfolio'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="rightHeading">',
'after_title' => '</div>'));
}
add_action('init', 'sp_rightSidebar');
Sidebar theme file:
<div id="rightColumn">
<?php if (!function_exists('sp_rightSidebar') || !sp_rightSidebar()): ?>
sp_rightSidebar is waiting.
<?php else: ?>
<?php dynamic_sidebar('sp_rightSidebar'); ?>
<?php endif; ?>
</div>
No matter what I do, it always displays "sp_rightSidebar is waiting on the sidebar", I have tested my widget plugin with other themes and it works fine, so it has to be something about my sidebar theme file/not registering the sidebar correctly I'm assuming. The "Right Sidebar" widget area does display in the widgets area in the admin panel, however anything added does not stay there.
I hate to be the guy who dumps his code asking for people to take a look, but if you see anything that could be wrong, I'd appreciate your input.
Thanks!