JonH
02-11-2010, 05:36 AM
I'm trying to make a sidebar widget that can show user's Twitter activity via Twitter API. I'm able to get the plugin to work as a "non widget" but if I use the widget public function I get the following fatal error:
Notice: Undefined variable: h in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64
Fatal error: Call to a member function getSetting() on a non-object in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64
Here's the twitter_widget.php
class TwitterWidget
{
/**
* Default settings on install
*/
public function install_plugin($h)
{
// widget
$h->addWidget('twitter_widget', 'twitter_widget', ''); // plugin name, function name, optional arguments
// Default settings
if (!$h->getSetting('twitter_widget_username')) { $h->updateSetting('twitter_widget_username', ''); }
if (!$h->getSetting('twitter_widget_password')) { $h->updateSetting('twitter_widget_password', ''); }
}
public function header_include($h)
{
$h->includeCss('twitter_widget');
}
public function widget_twitter_widget()
{
// your twitter username and password
$twitter_widget_username = $h->getSetting('twitter_widget_username');
$twitter_widget_password = $h->getSetting('twitter_widget_password');
// for Twitterlibphp
$twitter_widget_username = $username;
$twitter_widget_password = $password;
// include Twitterlibphp
require_once(PLUGINS . 'twitter_widget/libs/twitter_lib.php');
// initialize the twitter class
$twitter = new Twitter($username, $password);
// fetch your profile in xml format
$xml = $twitter->getFriendsTimeline();
// fetch your session xml format
$twitter_status = new SimpleXMLElement($xml);
// show twitter widget template
echo '<div class="twitter_container">';
foreach($twitter_status->status as $status){
echo '<div class="twitter_status">';
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo '<a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a>: ';
}
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at"><strong>Posted at:</strong> '.$status->created_at.'</div>';
echo '</div>';
echo '</div>';
}
}
}
Going by the Hello_world widget doc you don't have to make a plugin hook but something like public function "widget_twitter_widget()" (no $h) but I'm guessing that seems to keep the $h->getSetting to get the username and password. :confused: Thanks.
Notice: Undefined variable: h in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64
Fatal error: Call to a member function getSetting() on a non-object in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64
Here's the twitter_widget.php
class TwitterWidget
{
/**
* Default settings on install
*/
public function install_plugin($h)
{
// widget
$h->addWidget('twitter_widget', 'twitter_widget', ''); // plugin name, function name, optional arguments
// Default settings
if (!$h->getSetting('twitter_widget_username')) { $h->updateSetting('twitter_widget_username', ''); }
if (!$h->getSetting('twitter_widget_password')) { $h->updateSetting('twitter_widget_password', ''); }
}
public function header_include($h)
{
$h->includeCss('twitter_widget');
}
public function widget_twitter_widget()
{
// your twitter username and password
$twitter_widget_username = $h->getSetting('twitter_widget_username');
$twitter_widget_password = $h->getSetting('twitter_widget_password');
// for Twitterlibphp
$twitter_widget_username = $username;
$twitter_widget_password = $password;
// include Twitterlibphp
require_once(PLUGINS . 'twitter_widget/libs/twitter_lib.php');
// initialize the twitter class
$twitter = new Twitter($username, $password);
// fetch your profile in xml format
$xml = $twitter->getFriendsTimeline();
// fetch your session xml format
$twitter_status = new SimpleXMLElement($xml);
// show twitter widget template
echo '<div class="twitter_container">';
foreach($twitter_status->status as $status){
echo '<div class="twitter_status">';
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo '<a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a>: ';
}
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at"><strong>Posted at:</strong> '.$status->created_at.'</div>';
echo '</div>';
echo '</div>';
}
}
}
Going by the Hello_world widget doc you don't have to make a plugin hook but something like public function "widget_twitter_widget()" (no $h) but I'm guessing that seems to keep the $h->getSetting to get the username and password. :confused: Thanks.