How To create Widget In Wordpress

In this tutorial, we will create a simple widget that just greets visitors. Take a look at this code and then paste it in your site-specific plugin to see it in action.


// Creating the widget 
class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget', 

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'), 

// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), ) 
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}
  
// Widget Backend 
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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 esc_attr( $title ); ?>" />
</p>
<?php 
}
 
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
 register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

/*---------------------------------------------------------------*/

Now go to Appearance » Widgets, drag and drop WPBeginner Widget in your sidebar to see this custom widget in action.
Simple wasn’t it? First we created a custom widget. Then we defined what that widget does and how to display the widget back-end. Then we defined how to handle changes made to widget. Lastly, we registered and loaded the widget.


Import Gmail contacts using PHP


In this tutorial, we can import Gmail contacts using PHP.
Kindly follow below steps.


  <?php   error_reporting(E_ALL);   $user = "Your Gmail ID"; // Enter your gmail ID   $password = "Your Gmail Password"; // Enter your Gmail account password.       // ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html       // step 1: login   $login_url = "https://www.google.com/accounts/ClientLogin";   $fields = array(   'Email' => $user,   'Passwd' => $password,   'service' => 'cp', // <== contact list service code   'source' => 'test-google-contact-grabber',   'accountType' => 'GOOGLE',   );       $curl = curl_init();   curl_setopt($curl, CURLOPT_URL,$login_url);   curl_setopt($curl, CURLOPT_POST, 1);   curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   $result = curl_exec($curl);       $returns = array();       foreach (explode("\n",$result) as $line)   {   $line = trim($line);   if (!$line) continue;   list($k,$v) = explode("=",$line,2);       $returns[$k] = $v;   }       curl_close($curl);       // step 2: grab the contact list   $feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";       $header = array(   'Authorization: GoogleLogin auth=' . $returns['Auth'],   );       $curl = curl_init();   curl_setopt($curl, CURLOPT_URL, $feed_url);   curl_setopt($curl, CURLOPT_HTTPHEADER, $header);   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);       $result = curl_exec($curl);   curl_close($curl);       $data = json_decode($result);       $contacts = array();       foreach ($data->feed->entry as $entry)   {   $contact = new stdClass();   $contact->title = $entry->title->{'$t'};   $contact->email = $entry->{'gd$email'}[0]->address;   $contacts[] = $contact;   }   echo "<pre>";   print_r($contacts);

PHP CLOSING TAG TIPS


The PHP closing tag?> ) on a PHP document is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. I think you have faced this frustrating issues and had to waste your valuable time to debug. For this reason, all PHP files shouldOMIT the closing PHP tag, and instead use a comment block to mark the end of file and it’s location relative to the application root. This allows you to still identify a file as being complete and not truncated. Here is an example:
BAD PRACTICE:
1<?php 
2echo "Here's my code!"
3?>
GOOD PRACTICE:
1<?php 
2echo "Here's my code!";
Also, while using php code into html pages it is not recommended that you use php short form (<? ?>) because this may not support if it is configured in the php.ini. So, if you use this and found your application is not working and in the same time you can’t modify the php.ini then you have to change those which will kill lots of your time! So best is always use: