Skip to content

Instantly share code, notes, and snippets.

@jamesmm77
Last active January 10, 2023 09:20
Show Gist options
  • Save jamesmm77/7073364 to your computer and use it in GitHub Desktop.
Save jamesmm77/7073364 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Local Business Microdata Widget
Plugin URI: http://mywpcms.com/local-business-microdata-widget/
Description: A widget that displays business contact info with microdata markup for the Local Business type from Schema.org.
Version: 0.1.0
Author: James Maabadi
Author URI: https://plus.google.com/u/0/+JamesMaabadi/posts
License: GPL2
*/
// Register the Widget (mywpcms_local_microdata_widget)
add_action('widgets_init', create_function('', 'return register_widget("mywpcms_local_microdata_widget");' ) );
class mywpcms_local_microdata_widget extends WP_Widget {
// constructor
function mywpcms_local_microdata_widget() {
parent::WP_Widget(
false,
$name = __('Local Business Microdata', 'mywpcms_lbmw') ,
array( 'description' => __( 'Display business contact info with microdata as a widget', 'mywpcms_lbmw' ), )
);
}
// widget form creation
function form($instance) {
// Check values
if( $instance) {
$title=esc_attr($instance['title']);
$lbm_company_name=esc_attr($instance['lbm_company_name']);
$lbm_company_desc=esc_attr($instance['lbm_company_desc']);
$lbm_company_phone=esc_attr($instance['lbm_company_phone']);
$lbm_company_fax=esc_attr($instance['lbm_company_fax']);
$lbm_company_email=esc_attr($instance['lbm_company_email']);
$lbm_company_website=esc_attr($instance['lbm_company_website']);
$lbm_company_street=esc_attr($instance['lbm_company_street']);
$lbm_company_city=esc_attr($instance['lbm_company_city']);
$lbm_company_state=esc_attr($instance['lbm_company_state']);
$lbm_company_zip=esc_attr($instance['lbm_company_zip']);
$lbm_company_country=esc_attr($instance['lbm_company_country']);
$lbm_company_lat=esc_attr($instance['lbm_company_lat']);
$lbm_company_long=esc_attr($instance['lbm_company_long']);
$lbm_text_before=esc_attr($instance['lbm_text_before']);
$lbm_text_after=esc_attr($instance['lbm_text_after']);
} else {
$title='';
$lbm_company_name='';
$lbm_company_desc='';
$lbm_company_phone='';
$lbm_company_fax='';
$lbm_company_email='';
$lbm_company_website='';
$lbm_company_street='';
$lbm_company_city='';
$lbm_company_state='';
$lbm_company_zip='';
$lbm_company_country='';
$lbm_company_lat='';
$lbm_company_long='';
$lbm_text_before='';
$lbm_text_after='';
}
?>
<p>All fields are optional. Blank fields are not shown.</p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_name'); ?>"><?php _e('Business Name:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_name'); ?>" name="<?php echo $this->get_field_name('lbm_company_name'); ?>" type="text" value="<?php echo $lbm_company_name; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_desc'); ?>"><?php _e('Description:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_desc'); ?>" name="<?php echo $this->get_field_name('lbm_company_desc'); ?>" type="text" value="<?php echo $lbm_company_desc; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_phone'); ?>"><?php _e('Phone:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_phone'); ?>" name="<?php echo $this->get_field_name('lbm_company_phone'); ?>" type="text" value="<?php echo $lbm_company_phone; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_fax'); ?>"><?php _e('Fax:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_fax'); ?>" name="<?php echo $this->get_field_name('lbm_company_fax'); ?>" type="text" value="<?php echo $lbm_company_fax; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_email'); ?>"><?php _e('Email:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_email'); ?>" name="<?php echo $this->get_field_name('lbm_company_email'); ?>" type="text" value="<?php echo $lbm_company_email; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_website'); ?>"><?php _e('Website:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_website'); ?>" name="<?php echo $this->get_field_name('lbm_company_website'); ?>" type="text" value="<?php echo $lbm_company_website; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_street'); ?>"><?php _e('Street:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_street'); ?>" name="<?php echo $this->get_field_name('lbm_company_street'); ?>" type="text" value="<?php echo $lbm_company_street; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_city'); ?>"><?php _e('City:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_city'); ?>" name="<?php echo $this->get_field_name('lbm_company_city'); ?>" type="text" value="<?php echo $lbm_company_city; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_state'); ?>"><?php _e('State:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_state'); ?>" name="<?php echo $this->get_field_name('lbm_company_state'); ?>" type="text" value="<?php echo $lbm_company_state; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_zip'); ?>"><?php _e('Zip:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_zip'); ?>" name="<?php echo $this->get_field_name('lbm_company_zip'); ?>" type="text" value="<?php echo $lbm_company_zip; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_lat'); ?>"><?php _e('Lat:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_lat'); ?>" name="<?php echo $this->get_field_name('lbm_company_lat'); ?>" type="text" value="<?php echo $lbm_company_lat; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_company_long'); ?>"><?php _e('Long:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_company_long'); ?>" name="<?php echo $this->get_field_name('lbm_company_long'); ?>" type="text" value="<?php echo $lbm_company_long; ?>" />
</p>
<p>
<a href="http://itouchmap.com/latlong.html" target="_blank"><?php _e('Get Lat/Long', 'mywpcms_lbmw'); ?></a>
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_text_before'); ?>"><?php _e('Text/HTML Before:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_text_before'); ?>" name="<?php echo $this->get_field_name('lbm_text_before'); ?>" type="text" value="<?php echo $lbm_text_before; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('lbm_text_after'); ?>"><?php _e('Text/HTML After:', 'mywpcms_lbmw'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lbm_text_after'); ?>" name="<?php echo $this->get_field_name('lbm_text_after'); ?>" type="text" value="<?php echo $lbm_text_after; ?>" />
</p>
<?php
}
// update widget
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['title'] = strip_tags($new_instance['title']);
$instance['lbm_company_name']=strip_tags($new_instance['lbm_company_name']);
$instance['lbm_company_desc']=strip_tags($new_instance['lbm_company_desc']);
$instance['lbm_company_phone'] =strip_tags($new_instance['lbm_company_phone']);
$instance['lbm_company_fax'] =strip_tags($new_instance['lbm_company_fax']);
$instance['lbm_company_street']=strip_tags($new_instance['lbm_company_street']);
$instance['lbm_company_city'] =strip_tags($new_instance['lbm_company_city']);
$instance['lbm_company_state'] =strip_tags($new_instance['lbm_company_state']);
$instance['lbm_company_zip'] =strip_tags($new_instance['lbm_company_zip']);
$instance['lbm_company_lat'] =strip_tags($new_instance['lbm_company_lat']);
$instance['lbm_company_long']=strip_tags($new_instance['lbm_company_long']);
$instance['lbm_company_email'] =strip_tags($new_instance['lbm_company_email']);
$instance['lbm_company_website']=strip_tags($new_instance['lbm_company_website']);
$instance['lbm_text_before']=($new_instance['lbm_text_before']);
$instance['lbm_text_after'] =($new_instance['lbm_text_after']);
return $instance;
}
// display widget
function widget($args, $instance) {
extract( $args );
// these are the widget options
$title = apply_filters('widget_title', $instance['title']);
$lbm_company_name=$instance['lbm_company_name'];
$lbm_company_desc=$instance['lbm_company_desc'];
$lbm_company_phone =$instance['lbm_company_phone'];
$lbm_company_fax =$instance['lbm_company_fax'];
$lbm_company_street=$instance['lbm_company_street'];
$lbm_company_city =$instance['lbm_company_city'];
$lbm_company_state =$instance['lbm_company_state'];
$lbm_company_zip =$instance['lbm_company_zip'];
$lbm_company_lat =$instance['lbm_company_lat'];
$lbm_company_long =$instance['lbm_company_long'];
$lbm_company_email =$instance['lbm_company_email'];
$lbm_company_website=$instance['lbm_company_website'];
$lbm_text_before=$instance['lbm_text_before'];
$lbm_text_after =$instance['lbm_text_after'];
echo $before_widget;
// Display the widget
echo '<div class="widget-text wp_widget_plugin_box">';
echo '<div itemscope itemtype="http://schema.org/LocalBusiness">';
if ( $lbm_use_name_title && $lbm_company_name ) {
echo $before_title . '<span itemprop="name" class="name">'.$lbm_company_name.'</span>' . $after_title;
} else {
if ( $title ) {
echo $before_title . $title . $after_title;
}
}
if ( $lbm_text_before ) echo $lbm_text_before;
if ( !$lbm_use_name_title && $lbm_company_name ) {
echo '<p itemprop="name" class="name">'.$lbm_company_name.'</p>';
}
if ( $lbm_company_desc ) echo '<p itemprop="description" class="description">'.$lbm_company_desc.'</p>';
if ( $lbm_company_phone || $lbm_company_phone2 || $lbm_company_fax) {
echo '<p class="phones">';
if ($lbm_company_phone) echo 'Phone: <span itemprop="telephone">'.$lbm_company_phone.'</span><br />';
if ($lbm_company_fax) echo 'Fax: <span itemprop="faxNumber">'.$lbm_company_fax.'</span><br />';
echo '</p>';
}
if ( $lbm_company_street ) {
echo '<p itemtype="http://schema.org/PostalAddress" itemscope="" itemprop="address" class="address">';
echo '<span itemprop="streetAddress">'.$lbm_company_street.'</span><br />';
echo '<span itemprop="addressLocality">'.$lbm_company_city.'</span>, <span itemprop="addressRegion">'.$lbm_company_state.'</span> <span itemprop="postalCode">'.$lbm_company_zip.'</span>';
echo '</p>';
}
if ($lbm_company_email) echo '<p class="email">Email: <span itemprop="email">'.$lbm_company_email.'</span></p>';
if ($lbm_company_website) echo '<p class="website">Url: <span itemprop="url">'.$lbm_company_website.'</span></p>';
if ( $lbm_company_lat && $lbm_company_long ) {
echo '<div itemtype="http://schema.org/GeoCoordinates" itemscope="" itemprop="geo">';
echo '<meta itemprop="latitude" content="'.$lbm_company_lat.'" />';
echo '<meta itemprop="longitude" content="'.$lbm_company_long.'" />';
echo '</div>';
}
if ( $lbm_text_after ) echo $lbm_text_after;
echo '</div>';
echo '</div>'; // widget-text end
echo $after_widget;
}
}
?>
@senlin
Copy link

senlin commented Nov 4, 2013

Hi @jamesmm77 as per our Google+ PM here is the link to my fork of your cool plugin. In the description I have explained what I have done: https://gist.github.com/senlin/7296376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment