Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active March 31, 2016 21:31
Show Gist options
  • Save igorbenic/4e19990f74ed911a91bd37a94a231286 to your computer and use it in GitHub Desktop.
Save igorbenic/4e19990f74ed911a91bd37a94a231286 to your computer and use it in GitHub Desktop.
How to Create Sharing Buttons similar to Jetpack | http://www.ibenic.com/create-sharing-buttons-similar-jetpack/
<?php
function my_share_render_in_content( $content ) {
ob_start();
?>
<ul id="my_share" class="my_share">
<li>
<a href="<?php the_permalink();?>?my_share=facebook">Facebook</a>
</li>
<li>
<a href="<?php the_permalink();?>?my_share=twitter">Twitter</a>
</li>
</ul>
<?php
$content .= ob_get_contents();
ob_end_clean();
return $content;
}
add_action( 'the_content', 'my_share_render_in_content');
<?php
class WordPressShare_Facebook extends WordPressShare {
public $share_url = 'https://www.facebook.com/sharer/sharer.php';
public function get_link() {
$url = $this->share_url . '?u=' . $this->get_url() . '&t=' . $this->get_text();
return $url;
}
}
<?php
function my_share_social( $social ) {
$social_media = array(
'facebook' => 'WordPressShare_Facebook',
'twitter' => 'WordPressShare_Twitter' );
if( isset( $social_media[ $social ] ) ) {
$social_media_class = $social_media[ $social ];
$social_object = new $social_media_class();
$social_object->share();
}
}
(function($) {
$( document ).ready( function(){
$("#my_share a").on('click', function(e){
e.preventDefault();
var href = $( this ).attr("href");
window.open( href, 'Share', 'width=600, height=400');
})
});
})(jQuery);
<?php
function my_share_redirect() {
if( isset( $_GET['my_share'] ) ){
my_share_social( $_GET['my_share'] );
}
}
add_action( 'template_redirect', 'my_share_redirect' );
.my_share {
padding:0;
margin:0;
list-style: none;
display: block;
width:100%;
}
.my_share:after {
display: table;
content: '';
clear: both;
}
.my_share li {
float: left;
}
.my_share a {
padding: 0.5em .75em;
background-color:white;
color: black;
border: 1px solid gray;
box-shadow: 0px 1px 1px #ccc;
}
.my_share a:hover,
.my_share a:focus {
background-color:black;
color:white;
}
<?php
class WordPressShare_Twitter extends WordPressShare {
public $share_url = 'https://twitter.com/intent/tweet';
public function get_link() {
$url = $this->share_url . '?url=' . $this->get_url() . '&text=' . $this->get_text();
return $url;
}
}
<?php
abstract class WordPressShare {
/**
* Text to be shared
* @var string
*/
public $text = '';
/**
* URL to be shared
* @var string
*/
public $url = '';
/**
* Social Media URL that is used for sharing
* @var string
*/
public $share_url = '';
/**
* Post Object
* @var mixed
*/
public $post = null;
/**
* Asign the object and meta data
*/
public function __construct() {
$this->post = get_post( get_the_id() );
if( $this->post ){
$this->get_meta();
}
}
// ...
}
<?php
abstract class WordPressShare {
//...
/**
* Assigning the meta from the Post Object
* @return void
*/
protected function get_meta() {
$this->text = $this->post->post_title;
$this->url = get_permalink( $this->post );
}
}
<?php
abstract class WordPressShare {
//...
/**
* Get the URL
* @return string
*/
protected function get_url(){
return $this->url;
}
/**
* Get the URL encoded text
* @return string
*/
protected function get_text() {
return urlencode( $this->text );
}
/**
* Get the link to which we will redirect our visitor
* @return string
*/
public function get_link() {
}
/**
* Redirect the visitor to the sharing link
* @return void
*/
public function share() {
$link = $this->get_link();
if( $link ){
wp_redirect( $link );
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment