Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregrickaby/67205ca4bf61beb7d255 to your computer and use it in GitHub Desktop.
Save gregrickaby/67205ca4bf61beb7d255 to your computer and use it in GitHub Desktop.
Social Icons Widget
<?php
/*
Plugin Name: Social Icons Widget
Description: A simple widget to display social icons from Font Awesome.
Version: 1.0.0
Author: Greg Rickaby
Author URI: http://gregrickaby.com
License: GPLv2
Text Domain: textdomain
*/
class GR_Social_Icons extends WP_Widget {
public function __construct() {
parent::__construct(
'social_icons',
__( 'Social Icons', 'textdomain' ),
array( 'description' => __( 'D simple widget to display social icons from Font Awesome.,textdomain' ) )
);
add_action( 'wp_enqueue_styles', array( $this, 'font_awesome' ) );
}
/**
* Register and enqueue Font Awesome.
*/
public function font_awesome() {
wp_register_style( 'font-awesome', '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css', array(), null );
wp_enqueue_style( 'font-awesome' );
}
/**
* Front-end display of widget.
*/
public function widget( $args, $instance ) {
// Extract options
extract( $args );
// Set variables
$title = $instance['title'];
$twitter = $instance['twitter'];
$facebook = $instance['facebook'];
$instagram = $instance['instagram'];
$google = $instance['google'];
echo $before_widget;
if ( $title ) {
echo $before_title . esc_html( $title ) . $after_title;
}
?>
<ul class="social-icons">
<?php if ( $twitter ) : ?>
<li class="social-icon"><a href="<?php echo esc_url( $twitter ); ?>" target="_blank"><i class="fa fa-twitter-square"></i></li>
<?php endif; ?>
<?php if ( $facebook ) : ?>
<li class="social-icon"><a href="<?php echo esc_url( $facebook ); ?>" target="_blank"><i class="fa fa-facebook-square"></i></li>
<?php endif; ?>
<?php if ( $instagram ) : ?>
<li class="social-icon"><a href="<?php echo esc_url( $instagram ); ?>" target="_blank"><i class="fa fa-instagram"></i></li>
<?php endif; ?>
<?php if ( $google ) : ?>
<li class="social-icon"><a href="<?php echo esc_url( $google ); ?>" target="_blank"><i class="fa fa-google-plus-square"></i></li>
<?php endif; ?>
</ul>
<?php
echo $after_widget;
}
/**
* Back-end widget form with defaults.
*/
public function form( $instance ) {
// Set default options
$instance = wp_parse_args( (array) $instance,
array(
'title' => '',
'twitter' => '',
'facebook' => '',
'instagram' => '',
'google' => ''
)
);
?>
<p>
<label><?php _e( 'Title:', 'textdomain' ); ?></label>
<input class="widefat" type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_html( $instance['title'] ); ?>" placeholder="Optional" />
</p>
<p>
<label><?php _e( 'Twitter:', 'textdomain' ); ?></label>
<input class="widefat" type="url" name="<?php echo $this->get_field_name( 'twitter' ); ?>" id="<?php echo $this->get_field_id( 'twitter' ); ?>" value="<?php echo esc_url( $instance['twitter'] ); ?>" placeholder="https://twitter.com/username" />
</p>
<p>
<label><?php _e( 'Facebook:', 'textdomain' ); ?></label>
<input class="widefat" type="url" name="<?php echo $this->get_field_name( 'facebook' ); ?>" id="<?php echo $this->get_field_id( 'facebook' ); ?>" value="<?php echo esc_url( $instance['facebook'] ); ?>" placeholder="https://www.facebook.com/username" />
</p>
<p>
<label><?php _e( 'Instagram:', 'textdomain' ); ?></label>
<input class="widefat" type="url" name="<?php echo $this->get_field_name( 'instagram' ); ?>" id="<?php echo $this->get_field_id( 'instagram' ); ?>" value="<?php echo esc_url( $instance['instagram'] ); ?>" placeholder="https://instagram.com/username" />
</p>
<p>
<label><?php _e( 'Google+:', 'textdomain' ); ?></label>
<input class="widefat" type="url" name="<?php echo $this->get_field_name( 'google' ); ?>" id="<?php echo $this->get_field_id( 'google' ); ?>" value="<?php echo esc_url( $instance['google'] ); ?>" placeholder="https://plus.google.com/+username" />
</p>
<?php
}
/**
* Update form values as they are saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
// Sanitize all text fields.
foreach ( array( 'title' ) as $key => $value ) {
$instance[$value] = esc_html( $new_instance[$value] );
}
// Sanitize all url fields.
foreach ( array( 'twitter', 'facebook', 'instagram', 'google' ) as $key => $value ) {
$instance[$value] = esc_url( $new_instance[$value] );
}
return $instance;
}
} // end GR_Social_Icons
/**
* Register the widget with WordPress.
*/
function gr_social_icons_widget() {
register_widget( 'GR_Social_Icons' );
}
add_action( 'widgets_init', 'gr_social_icons_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment