Instantly share code, notes, and snippets.
Last active
September 18, 2017 06:27
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save patric-boehner/44d2ed6be30ada7bcd7eb2693c37ab97 to your computer and use it in GitHub Desktop.
Social Media Link Widget with Icons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Social Widget | |
| * Based on Bill Erickson's example from his Core Functionality plugin example. | |
| * An older version of Bill's code had a similar style widget example. | |
| * | |
| * @package Core Functionality | |
| * @since 1.0.0 | |
| * @link https://github.com/billerickson/Core-Functionality | |
| * @author Bill Erickson <[email protected]> | |
| * @author Patrick Boehner <[email protected]> | |
| * @copyright Copyright (c) 2011, Bill Erickson | |
| * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
| */ | |
| //* Block Acess | |
| //********************************************** | |
| if( !defined( 'ABSPATH' ) ) exit; | |
| // Setup Simple Social Widget | |
| //********************************************** | |
| class PB_Social_Widget extends WP_Widget { | |
| /** | |
| * Constructor | |
| * @return void | |
| **/ | |
| function __construct() { | |
| $widget_ops = array( | |
| 'classname' => 'widget_social_links', | |
| 'description' => 'A simple social media icon widget' | |
| ); | |
| parent::__construct( 'social-widget', 'Social Media Links Widget', $widget_ops ); | |
| } | |
| /** | |
| * Social Options | |
| * | |
| */ | |
| function social_options() { | |
| return array( | |
| 'facebook' => 'Facebook', | |
| 'youtube' => 'Youtube', | |
| 'vimeo' => 'Vimeo', | |
| 'linkedin' => 'LinkedIn', | |
| 'instagram' => 'Instagram', | |
| 'twitter' => 'Twitter', | |
| 'pinterest' => 'Pinterest', | |
| 'yelp' => 'Yelp', | |
| ); | |
| } | |
| /** | |
| * Outputs the HTML for this widget. | |
| * Includes schema for soical media porfiles as outlined by google | |
| * | |
| * @param array An array of standard parameters for widgets in this theme | |
| * @param array An array of settings for this widget instance | |
| * @return void Echoes it's output | |
| **/ | |
| function widget( $args, $instance ) { | |
| extract( $args, EXTR_SKIP ); | |
| echo $before_widget; | |
| if( $instance['title'] ) | |
| echo $before_title . esc_html( $instance['title'] ) . $after_title; | |
| echo '<div class="social-links" itemscope itemtype="http://schema.org/Organization">'; | |
| echo '<span class="screen-reader-text">Social media links</span>'; | |
| echo '<link itemprop="url" href="' .get_bloginfo( 'url' ). '">'; | |
| echo '<ul class=social-links-list>'; | |
| $socials = $this->social_options(); | |
| foreach( $socials as $key => $label ) { | |
| if( !empty( $instance[$key] ) ) | |
| echo '<li class="social-item"><a class="social-icon icon-' . $key . '" itemprop="sameAs" href="' . esc_url( $instance[$key] ) . '" target="_blank" rel="noopener noreferrer" aria-label="' .$key. ' - Opens in new window"></a></li>'; | |
| } | |
| echo '</ul></div>'; | |
| echo $after_widget; | |
| } | |
| /** | |
| * Deals with the settings when they are saved by the admin. Here is | |
| * where any validation should be dealt with. | |
| * | |
| * @param array An array of new settings as submitted by the admin | |
| * @param array An array of the previous settings | |
| * @return array The validated and (if necessary) amended settings | |
| **/ | |
| function update( $new_instance, $old_instance ) { | |
| $instance = $old_instance; | |
| $instance['title'] = esc_attr( $new_instance['title'] ); | |
| $socials = $this->social_options(); | |
| foreach( $socials as $key => $label ) | |
| $instance[$key] = esc_url( $new_instance[$key] ); | |
| return $instance; | |
| } | |
| /** | |
| * Displays the form for this widget on the Widgets page of the WP Admin area. | |
| * | |
| * @param array An array of the current settings for this widget | |
| * @return void Echoes it's output | |
| **/ | |
| function form( $instance ) { | |
| $socials = $this->social_options(); | |
| $defaults = array( 'title' => '' ); | |
| foreach( $socials as $key => $label ) | |
| $defaults[$key] = ''; | |
| $instance = wp_parse_args( (array) $instance, $defaults ); | |
| echo '<p><label for="' . $this->get_field_id( 'title' ) . '">Title: <input class="widefat" id="' . $this->get_field_id( 'title' ) .'" name="' . $this->get_field_name( 'title' ) . '" value="' . esc_attr( $instance['title'] ) . '" /></label></p>'; | |
| foreach( $socials as $key => $label ) | |
| echo '<p><label for="' . $this->get_field_id( $key ) . '">' . $label . ' URL: <input class="widefat" id="' . $this->get_field_id( $key ) .'" name="' . $this->get_field_name( $key ) . '" value="' . esc_url( $instance[$key] ) . '" /></label></p>'; | |
| } | |
| } | |
| //* Register the widget | |
| add_action( 'widgets_init', 'pb_register_social_widget' ); | |
| function pb_register_social_widget() { | |
| register_widget('PB_Social_Widget'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment