In the last days we digged into deeps of Tripadvisor API adding a wordpress widget to our Koh Samui directory project: samui.samuiweb.net
Until now did not have time to package the function as a wordpress plugin, besides can share the work done.
First of all: exists already a good plugin for wordpress.
But it lacks of one fundamental feature: the tripdavisor ID parameter has to be inserted in configuration: the widget can not be inserted once for working with several posts, that is what we would like to do.
Update: the code below is outdated, you can download a zipfile in wordpress plugin format.
Our solution:
class tripadvisor extends WP_Widget { function tripadvisor() { //Constructor $widget_ops = array('classname' => 'widget Tripadvisor', 'description' => 'Tripadvisor widget' ); $this->WP_Widget('tripadvisor', 'SAMUIWEB → Tripadvisor', $widget_ops); } function widget($args, $instance) { // prints the widget extract($args, EXTR_SKIP); echo $before_widget; $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); ?> <?php if($title){?> <h3 class="clearfix"><span class="fl"><?php echo $title; ?></span></h3> <?php } global $wpdb,$rating_table_name,$thumb_url,$post; $sql = " select $wpdb->postmeta.meta_value as url from $wpdb->postmeta where $wpdb->postmeta.post_id = '".$post->ID."' and $wpdb->postmeta.meta_key = 'tripadvisor_url'"; $res = $wpdb->get_results($sql); if ( $res[0]->url != "" ) { // parse url $pattern = "/-d(\d+)/"; preg_match($pattern,$res[0]->url,&$larr); if ( is_numeric($larr[1]) ) { echo "<div id=\"TA_selfserveprop530\" class=\"TA_selfserveprop\" style=\"width: 250px;\"><ul id=\"gHuY6Bgr\" class=\"TA_links b9NoNI3aGFHx\"><li id=\"FzfUc\ Cq1\" class=\"szp5kls9BkE\">reviews of <a target=\"_blank\" href=\"".$res[0]->url."\">".$post->post_title."</a></li></ul></div><script src=\"http://www.jscache.com/wejs?wtype=selfserveprop&uniq=530&locationId=".$larr[1]."&lang=en_US&rating=true&nreviews=5&writereviewlink=true&popIdx=true&iswide=true&border=false\"></script>"; } } echo $after_widget; } function update($new_instance, $old_instance) { //save the widget $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['post_number'] = strip_tags($new_instance['post_number']); return $instance; } function form($instance) { //widgetform in backend $instance = wp_parse_args( (array) $instance, array( 'title' => '') ); $title = strip_tags($instance['title']); ?> <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 attribute_escape($title); ?>" /> </label> </p> <?php } } register_widget('tripadvisor'); ?>
You can see in action here or here
Note that does not provide a SEO added value because it’s a tripadvisor javascript that load the html. On the other side Google does not see a duplicate content and you will be not penalized in ranking (obviously if your users )
August 27, 2014 at 10:18 am
to copy this code, mark me an error here: ” preg_match($pattern,$res[0]->url,&$larr);” , someone to spend the same?
thank you
August 30, 2014 at 10:50 am
Dear thank you very much for all your help !!!
and the module works perfectly me you gave me.
greetings
Francisco