I made this plugin to show reviews from yelp from my business, it is my first attempt and it works, but I’d like to share it with everyone.
When I add my settings on the code it works the way it is supposed to. But when I made the admin interface so people can use their own API keys and change the look to their best fitting, it won’t “save” the settings and will not work.
I am hoping someone could shed some light and help me out so I can share the code/plugin.
(by the way this is my first attempt to coding)
yelprt.php
<?php
/*
Plugin Name: Yelp Reviews Ticker
Plugin URI: http://www.google.com/
Description: Yelp Reviews Ticker
Version: 0.1
Author: Flavio
Author URI: http://www.google.com/
*/
function yelp_rt_widget($args, $widget_args = 1) {
extract( $args, EXTR_SKIP );
if ( is_numeric($widget_args) )
$widget_args = array( 'number' => $widget_args );
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
extract( $widget_args, EXTR_SKIP );
$options = get_option('yelprt_widget');
if ( !isset($options[$number]) )
return;
$title = $options[$number]['title']; // single value
$speed = $options[$number]['speed'];
$pause = $options[$number]['pause'];
$showitems = $options[$number]['showitems'];
$animation = $options[$number]['animation'];
$mousepause = $options[$number]['mousepause'];
//$height = $options[$number]['height'];
$direction = $options[$number]['direction'];
$yrtunsigned_url = $options[$number]['unsigned_url'];
$yrtconsumer_key = $options[$number]['consumer_key'];
$yrtconsumer_secret = $options[$number]['consumer_secret'];
$yrttoken = $options[$number]['token'];
$yrttoken_secret = $options[$number]['token_secret'];
echo $before_widget; // start widget display code
?><h2><?=$title?></h2><?php
//print $yrtunsigned_url;
//echo $options[$number]['unsigned_url'];
//******MY ADDITION
// From http://non-diligent.com/articles/yelp-apiv2-php-example/
//
// Enter the path that the oauth library is in relation to the php file
//require_once ('lib/OAuth.php');
$unsigned_url = $options[$number]['unsigned_url'];
$consumer_key = $options[$number]['consumer_key'];
$consumer_secret = $options[$number]['consumer_secret'];
$token = $options[$number]['token'];
$token_secret = $options[$number]['token_secret'];
require_once ('lib/OAuth.php');
// Token object built using the OAuth library
$token = new OAuthToken($token, $token_secret);
//$token = new OAuthToken($yrttoken, $yrttoken_secret);
// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
//$consumer = new OAuthConsumer($yrtconsumer_key, $yrtconsumer_secret);
// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
//$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $yrtunsigned_url);
// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);
//$oauthrequest->sign_request($signature_method, $yrtconsumer, $yrttoken);
// Get the signed URL
$signed_url = $oauthrequest->to_url();
// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);
// Handle Yelp response data
$response = json_decode($data);
$arr = (array) $response;
if(is_array($arr['reviews'])){
echo "<div id='yelprt'>";
echo "<ul id='list'>";
foreach($arr['reviews'] as $review){
echo "<li><div id='yelprt_table'>n";
echo " <div class='row'>n";
echo " <span class='yelprt_cell2'>n";
echo " <img src='" . $review->user->image_url . "' width='60px'/>n";
echo " <br />" . $review->user->name . "n";
echo " <br /><img src='" . $review->rating_image_small_url . "'/>n";
echo " </span>n";
echo " <span class='yelprt_cell1'>n";
echo " " . $review->excerpt . "n";
echo " <br />n";
echo " <span class='yelprt_smalltxt'>" . gmdate("m/d/Y", $review->time_created) . " more at <a href='".$yrtunsigned_url."'><img src='http://s3-media1.ak.yelpcdn.com/assets/2/www/img/14f29ad24935/map/miniMapLogo.png'/></a></span>n";
echo " </span>n";
echo " </div>n";
echo " </div></li>n";
}
echo "</div>";
}
echo $after_widget; // end widget display code
}
function yelp_rt_widget_control($widget_args) {
global $wp_registered_widgets;
static $updated = false;
if ( is_numeric($widget_args) )
$widget_args = array( 'number' => $widget_args );
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
extract( $widget_args, EXTR_SKIP );
$options = get_option('yelprt_widget');
if ( !is_array($options) )
$options = array();
if ( !$updated && !empty($_POST['sidebar']) ) {
$sidebar = (string) $_POST['sidebar'];
$sidebars_widgets = wp_get_sidebars_widgets();
if ( isset($sidebars_widgets[$sidebar]) )
$this_sidebar =& $sidebars_widgets[$sidebar];
else
$this_sidebar = array();
foreach ( (array) $this_sidebar as $_widget_id ) {
if ( 'yelp_rt_widget' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
if ( !in_array( "yelp-rt-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
unset($options[$widget_number]);
}
}
foreach ( (array) $_POST['yelp-rt'] as $widget_number => $yelprt_widget ) {
if ( !isset($yelprt_widget['title']) && isset($options[$widget_number]) ) // user clicked cancel
continue;
$title = strip_tags(stripslashes($yelprt_widget['title']));
$speed = $yelprt_widget['speed_value'];
$pause = $yelprt_widget['pause_value'];
$showitems = $yelprt_widget['showitems_value'];
$animation = $yelprt_widget['animation_value'];
$mousepause = $yelprt_widget['mousepause_value'];
//$height = $yelprt_widget['height_value'];
$direction = $yelprt_widget['direction_value'];
$yrtunsigned_url = $yelprt_widget['unsigned_url_value'];
$yrtconsumer_key = $yelprt_widget['consumer_key_value'];
$yrtconsumer_secret = $yelprt_widget['consumer_secret_value'];
$yrttoken = $yelprt_widget['token_value'];
$yrttoken_secret = $yelprt_widget['token_secret_value'];
// Pact the values into an array
$options[$widget_number] = compact( 'title', 'speed', 'pause', 'showitems', 'animation', 'mousepause', 'direction', 'unsigned_url', 'consumer_key', 'consumer_secret', 'token', 'token_secret' );
}
update_option('yelprt_widget', $options);
$updated = true;
}
if ( -1 == $number ) { // if it's the first time and there are no existing values
$title = 'Reviews';
$speed = '2500';
$pause = '6000';
$showitems = '2';
$animation = 'fade';
$mousepause = 'true';
//$height = '';
$direction = 'up';
$yrtunsigned_url = '';
$yrtconsumer_key = '';
$yrtconsumer_secret = '';
$yrttoken = '';
$yrttoken_secret = '';
$yrtnumber = '%i%';
} else { // otherwise get the existing values
$title = $options[$number]['title'];
$speed = $options[$number]['speed'];
$pause = $options[$number]['pause'];
$showitems = $options[$number]['showitems'];
$animation = $options[$number]['animation'];
$mousepause = $options[$number]['mousepause'];
//$height = $options[$number]['height'];
$direction = $options[$number]['direction'];
$yrtunsigned_url = $options[$number]['unsigned_url'];
$yrtconsumer_key = $options[$number]['consumer_key'];
$yrtconsumer_secret = $options[$number]['consumer_secret'];
$yrttoken = $options[$number]['token'];
$yrttoken_secret = $options[$number]['token_secret'];
}
print_r($options[$number]);
?>
<p><label>Widget Title</label><br /><input id="title_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][title]" type="text" value="<?=$title?>" /></p>
<p><label>Speed</label><br /><input id="speed_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][speed_value]" type="text" value="<?=$speed?>" /></p>
<p><label>Pause</label><br /><input id="pause_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][pause_value]" type="text" value="<?=$pause?>" /></p>
<p><label>Show Items</label><br /><input id="showitems_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][showitems_value]" type="text" value="<?=$showitems?>" /></p>
<p>
<label>Fade</label><br />
Yes <input id="animation_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][animation_value]" type="radio" <?php if($animation == 'fade') echo 'checked="checked"'; ?> value="fade" />
No <input id="animation_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][animation_value]" type="radio" <?php if($animation == '') echo 'checked="checked"'; ?> value="" />
</p>
<p>
<label>Mousepause</label><br />
Yes <input id="mousepause_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][mousepause_value]" type="radio" <?php if($mousepause == 'true') echo 'checked="checked"'; ?> value="true" />
No <input id="mousepause_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][mousepause_value]" type="radio" <?php if($mousepause == 'false') echo 'checked="checked"'; ?> value="false" />
</p>
<p>
<label>Direction</label><br />
Up <input id="direction_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][direction_value]" type="radio" <?php if($direction == 'up') echo 'checked="checked"'; ?> value="up" />
Down <input id="direction_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][direction_value]" type="radio" <?php if($direction == 'down') echo 'checked="checked"'; ?> value="down" />
</p>
<p><label>Business URL</label><br /><input id="unsigned_url_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][unsigned_url_value]" type="text" value="<?=$yrtunsigned_url?>" /></p>
<p><label>Consumer Key</label><br /><input id="consumer_key_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][consumer_key_value]" type="text" value="<?=$yrtconsumer_key?>" /></p>
<p><label>Consumer Secret</label><br /><input id="consumer_secret_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][consumer_secret_value]" type="text" value="<?=$yrtconsumer_secret?>" /></p>
<p><label>Token</label><br /><input id="token_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][token_value]" type="text" value="<?=$yrttoken?>" /></p>
<p><label>Token Secret</label><br /><input id="token_secret_value_<?php echo $number; ?>" name="yelp-rt[<?php echo $number; ?>][token_secret_value]" type="text" value="<?=$yrttoken_secret?>" /></p>
<input type="hidden" name="yelp-rt[<?php echo $number; ?>][submit]" value="1" />
<?php
}
function yelp_rt_widget_register() {
if ( !$options = get_option('yelprt_widget') )
$options = array();
$widget_ops = array('classname' => 'yelprt_widget', 'description' => __('Yelp Reviews Ticker'));
$control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'yelp-rt');
$name = __('Yelp Reviews Ticker Widget');
$id = false;
foreach ( (array) array_keys($options) as $o ) {
if ( !isset( $options[$o]['title'] ) )
continue;
$id = "yelp-rt-$o";
wp_register_sidebar_widget($id, $name, 'yelp_rt_widget', $widget_ops, array( 'number' => $o ));
wp_register_widget_control($id, $name, 'yelp_rt_widget_control', $control_ops, array( 'number' => $o ));
}
if ( !$id ) {
wp_register_sidebar_widget( 'yelp-rt-1', $name, 'yelp_rt_widget', $widget_ops, array( 'number' => -1 ) );
wp_register_widget_control( 'yelp-rt-1', $name, 'yelp_rt_widget_control', $control_ops, array( 'number' => -1 ) );
}
}
add_action('init', yelp_rt_widget_register, 1);
//add_action('widgets_init', create_function('', 'return register_widget("yelprt_widget");') );
add_action('wp_head', 'yelprt_head');
function yelprt_head($widget_args) {
global $wp_registered_widgets;
static $updated = false;
if ( is_numeric($widget_args) )
$widget_args = array( 'number' => $widget_args );
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
extract( $widget_args, EXTR_SKIP );
$options = get_option('yelprt_widget');
$title = $options[$number]['title'];
$speed = $options[$number]['speed'];
$pause = $options[$number]['pause'];
$showitems = $options[$number]['showitems'];
$animation = $options[$number]['animation'];
$mousepause = $options[$number]['mousepause'];
//$height = $options[$number]['height'];
$direction = $options[$number]['direction'];
$yrtunsigned_url = $options[$number]['unsigned_url'];
$yrtconsumer_key = $options[$number]['consumer_key'];
$yrtconsumer_secret = $options[$number]['consumer_secret'];
$yrttoken = $options[$number]['token'];
$yrttoken_secret = $options[$number]['token_secret'];
echo "<!-- Start Yelp Reviews Ticker -->n";
echo "<link type='text/css' href='" . plugins_url( 'css/yelprt.css' , __FILE__ ) . "' rel='stylesheet'/>n";
echo "<script type='text/javascript' src='" . plugins_url( 'lib/jquery.js' , __FILE__ ) . "'></script>n";
echo "<script type='text/javascript' src='" . plugins_url( 'lib/jquery.vticker-min.js' , __FILE__ ) . "'></script>n";
echo "<script type='text/javascript'>n";
echo " $(function(){n";
echo " $('#yelprt').vTicker({ n";
echo " speed: ".$speed.",n";
echo " pause: ".$pause.",n";
echo " animation: '".$animation."',n";
echo " mousePause: ".$mousepause.",n";
echo " direction: '".$direction."',n";
echo " showItems: ".$showitems."n";
echo " });n";
echo "});n";
echo "</script>n";
echo "<!-- End Yelp Reviews Ticker -->n";
}
?>
I know I still have a lot of code cleaning
var_dump($options);
right under this line–$options = get_option('yelprt_widget');
and tell me if$unsigned_url
is there.Indeed I was using a very old “plugin template” before wordpress had the $instance call.
so people used to make their own instances, wich is what I was doing.
I’ve cleaned up my code and got it working.
The only problem I encountered is that my css won’t work in the plugin once jQuery takes over.
But that is a different question.
here is my fix
I hope it helps someone else