Instantly share code, notes, and snippets.
Created
July 12, 2019 09:39
-
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 Matteo182/ffb35b9278e15704249aaf7ad5506518 to your computer and use it in GitHub Desktop.
Social share button widget for Wordpress
This file contains 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 media sharing button | |
/************************** */ | |
//creao una widget area | |
function tdm_social_post_widgets_init() { | |
register_sidebar( array( | |
'name' => __( 'Social in post', 'tdm_social' ), | |
'id' => 'tdm_social', | |
'before_widget' => '<div class="tdm-social-area">', | |
'after_widget' => '</div>', | |
'before_title' => '<h1>', | |
'after_title' => '</h1>', | |
) ); | |
} | |
add_action( 'widgets_init', 'tdm_social_post_widgets_init' ); | |
class tdm_social extends WP_Widget { | |
function tdm_social() { | |
$widget_ops = array( 'classname' => 'tdm_social', 'description' => __( 'TDM Social', 'wptheme' ) ); | |
$this->WP_Widget( 'tdm_social', __( 'TDM Social', 'wptheme' ), $widget_ops, $control_ops ); | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters( 'widget_title', $instance[ 'title' ] ); | |
// The following variable is for a checkbox option type | |
$Facebook = $instance[ 'Facebook' ] ? 'true' : 'false'; | |
$Twitter = $instance[ 'twitter' ] ? 'true' : 'false'; | |
$Buffer = $instance[ 'Buffer' ] ? 'true' : 'false'; | |
$LinkedIn = $instance[ 'LinkedIn' ] ? 'true' : 'false'; | |
$Pinterest = $instance[ 'Pinterest' ] ? 'true' : 'false'; | |
echo $before_widget; | |
if ( $title ) { | |
echo $before_title . $title . $after_title; | |
} | |
// Get current page URL | |
$tdmURL = urlencode(get_permalink()); | |
// Get current page title | |
$tdmTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8')), ENT_COMPAT, 'UTF-8'); | |
// $tdmTitle = str_replace( ' ', '%20', get_the_title()); | |
// Get Post Thumbnail for pinterest | |
$tdmThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); | |
// Construct sharing URL without using any script | |
$twitterURL = 'https://twitter.com/intent/tweet?text='.$tdmTitle.'&url='.$tdmURL.'&via=treehost.eu'; | |
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$tdmURL; | |
$bufferURL = 'https://bufferapp.com/add?url='.$rdmURL.'&text='.$tdmTitle; | |
$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$tdmURL.'&title='.$tdmTitle; | |
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$rdmURL.'&media='.$tdmThumbnail[0].'&description='.$tdmTitle; | |
// Retrieve the checkbox | |
// Add sharing button at the end of page/page content | |
$variable .= '<div class="tdm-social-social">'; | |
if( 'on' == $instance[ 'Facebook' ] ) { | |
$variable .= '<a class="tdm-social-link tdm-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>'; | |
} | |
if( 'on' == $instance[ 'twitter' ] ) { | |
$variable .= '<a class="tdm-social-link tdm-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>'; | |
} | |
if( 'on' == $instance[ 'Buffer' ] ) { | |
$variable .= '<a class="tdm-social-link tdm-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>'; | |
} | |
if( 'on' == $instance[ 'LinkedIn' ] ) { | |
$variable .= '<a class="tdm-link tdm-linkedin" href="'.$linkedInURL.'" target="_blank">LinkedIn</a>'; | |
} | |
if( 'on' == $instance[ 'Pinterest' ] ) { | |
$variable .= '<a class="tdm-link tdm-pinterest" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank">Pinterest</a>'; | |
$variable .= '</div>'; | |
} | |
echo $variable; | |
?> | |
<?php | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); | |
// The update for the variable of the checkbox | |
$instance[ 'Facebook' ] = $new_instance[ 'Facebook' ]; | |
$instance[ 'twitter' ] = $new_instance[ 'twitter' ]; | |
$instance[ 'Buffer' ] = $new_instance[ 'Buffer' ]; | |
$instance[ 'LinkedIn' ] = $new_instance[ 'LinkedIn' ]; | |
$instance[ 'Pinterest' ] = $new_instance[ 'Pinterest' ]; | |
return $instance; | |
} | |
function form( $instance ) { | |
$defaults = array( 'title' => __( ' ', 'wptheme' ), 'Facebook' => 'off', 'twitter' => 'off', 'Buffer' => 'off', 'LinkedIn' => 'off', 'Pinterest' => 'off' ); | |
$instance = wp_parse_args( ( array ) $instance, $defaults ); ?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>">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( $instance[ 'title' ] ); ?>" /> | |
</p> | |
<!-- The checkbox --> | |
<p> | |
<label> Scegli i network su cui condividere:</label><br> | |
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'Facebook' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'Facebook' ); ?>" name="<?php echo $this->get_field_name( 'Facebook' ); ?>" /> Facebook<br> | |
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'twitter' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" /> twitter<br> | |
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'Buffer' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'Buffer' ); ?>" name="<?php echo $this->get_field_name( 'Buffer' ); ?>" /> Buffer<br> | |
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'LinkedIn' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'LinkedIn' ); ?>" name="<?php echo $this->get_field_name( 'LinkedIn' ); ?>" /> LinkedIn<br> | |
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'Pinterest' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'Pinterest' ); ?>" name="<?php echo $this->get_field_name( 'Pinterest' ); ?>" /> Pinterest<br> | |
</p> | |
<?php | |
} | |
} | |
register_widget( 'tdm_social' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment