Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created April 17, 2014 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatch/11006935 to your computer and use it in GitHub Desktop.
Save jaredatch/11006935 to your computer and use it in GitHub Desktop.
social widget
<?php
/**
* Core Functionality Plugin
*
* @package CoreFunctionality
* @since 1.0.0
* @copyright Copyright (c) 2014, Bill Erickson & Jared Atchison
* @license GPL-2.0+
*/
/**
* Social widget
*
* @since 1.0.0
*/
class EA_Social_Widget extends WP_Widget {
/**
* Holds widget settings defaults, populated in constructor.
*
* @since 1.0.0
* @var array
*/
protected $defaults;
/**
* Constructor
*
* @since 1.0.0
*/
function __construct() {
// widget defaults
$this->defaults = array(
'title' => '',
'twitter' => '',
'facebook' => '',
'pinterest' => '',
'google' => '',
'linkedin' => '',
);
// widget basics
$widget_ops = array(
'classname' => 'ea-social-widget',
'description' => ''
);
// widget controls
$control_ops = array(
'id_base' => 'ea-social-widget',
//'width' => '400',
);
// load widget
$this->WP_Widget( 'ea-social-widget', 'Social Icons', $widget_ops, $control_ops );
}
/**
* Outputs the HTML for this widget.
*
* @since 1.0.0
* @param array $args An array of standard parameters for widgets in this theme
* @param array $instance An array of settings for this widget instance
*/
function widget( $args, $instance ) {
extract( $args );
// Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
echo $before_widget;
// Title
if ( !empty( $instance['title'] ) ) {
echo $before_title . $instance['title'] . $after_title;
}
echo '<ul>';
// Twitter
if ( !empty( $instance['twitter'] ) ) {
echo '<li class="twitter"><a href="' . esc_html( $instance['twitter'] ) . '" target="_blank">Twitter</a></li>';
}
// Facebook
if ( !empty( $instance['facebook'] ) ) {
echo '<li class="facebook"><a href="' . esc_html( $instance['facebook'] ) . '" target="_blank">Facebook</a></li>';
}
// Pinterest
if ( !empty( $instance['pinterest'] ) ) {
echo '<li class="pinterest"><a href="' . esc_html( $instance['pinterest'] ) . '" target="_blank">Pinterest</a></li>';
}
// Google+
if ( !empty( $instance['google'] ) ) {
echo '<li class="google"><a href="' . esc_html( $instance['google'] ) . '" target="_blank">Google+</a></li>';
}
// LinkedIn
if ( !empty( $instance['linkedin'] ) ) {
echo '<li class="linkedin"><a href="' . esc_html( $instance['linkedin'] ) . '" target="_blank">LinkedIn</a></li>';
}
echo '</ul>';
echo $after_widget;
}
/**
* Deals with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @since 1.0.0
* @param array $new_instance An array of new settings as submitted by the admin
* @param array $old_instance An array of the previous settings
* @return array The validated and (if necessary) amended settings
*/
function update( $new_instance, $old_instance ) {
$new_instance['title'] = strip_tags( $new_instance['title'] );
$new_instance['twitter'] = esc_url_raw( $new_instance['twitter'] );
$new_instance['facebook'] = esc_url_raw( $new_instance['facebook'] );
$new_instance['pinterest'] = esc_url_raw( $new_instance['pinterest'] );
$new_instance['google'] = esc_url_raw( $new_instance['google'] );
$new_instance['linkedin'] = esc_url_raw( $new_instance['linkedin'] );
return $new_instance;
}
/**
* Displays the form for this widget on the Widgets page of the WP Admin area.
*
* @since 1.0.0
* @param array $instance An array of the current settings for this widget
*/
function form( $instance ) {
// Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'twitter' ); ?>">Twitter:</label>
<input type="text" id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="<?php echo esc_attr( $instance['twitter'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'facebook' ); ?>">Facebook:</label>
<input type="text" id="<?php echo $this->get_field_id( 'facebook' ); ?>" name="<?php echo $this->get_field_name( 'facebook' ); ?>" value="<?php echo esc_attr( $instance['facebook'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'pinterest' ); ?>">Pinterest:</label>
<input type="text" id="<?php echo $this->get_field_id( 'pinterest' ); ?>" name="<?php echo $this->get_field_name( 'pinterest' ); ?>" value="<?php echo esc_attr( $instance['pinterest'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'google' ); ?>">Google+:</label>
<input type="text" id="<?php echo $this->get_field_id( 'google' ); ?>" name="<?php echo $this->get_field_name( 'google' ); ?>" value="<?php echo esc_attr( $instance['google'] ); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'linkedin' ); ?>">LinkedIn:</label>
<input type="text" id="<?php echo $this->get_field_id( 'linkedin' ); ?>" name="<?php echo $this->get_field_name( 'linkedin' ); ?>" value="<?php echo esc_attr( $instance['linkedin'] ); ?>" class="widefat" />
</p>
<?php
}
}
add_action( 'widgets_init', create_function( '', "register_widget('EA_Social_Widget');" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment