Instantly share code, notes, and snippets.
Last active
February 25, 2016 20:12
-
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 norcross/4114a916e838b454d2ff to your computer and use it in GitHub Desktop.
Use custom social links
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 | |
/** | |
* Get the array of social links. | |
* | |
* @param integer $post_id The post ID of the content to share. | |
* @param string $network Optional key to get one single network link instead of the whole array. | |
* | |
* @return array $sharelinks The array of links. | |
*/ | |
function rkv_get_social_share_links( $post_id = 0, $network = '' ) { | |
// Make sure we have a post ID. | |
if ( empty( $post_id ) ) { | |
return; | |
} | |
// Get my short link from the post ID. | |
$link = wp_get_shortlink( $post_id ); | |
// Create the text to include. | |
$text = str_replace( ' ', ' ', get_the_title( $post_id ) ); | |
// Build the facebook link. | |
$facebook = 'https://www.facebook.com/sharer/sharer.php?u=' . $link; | |
// Build the twitter link. | |
$twitter = 'https://twitter.com/intent/tweet?source=webclient&via=CLIENT-NAME&text=' . esc_attr( trim( $text ) ) . '&url=' . urlencode( $link ); | |
// Build the pinterest link. | |
$pinlink = 'https://pinterest.com/pin/create/button/?url=' . $link . '&description=' . esc_attr( trim( $text ) ) . ''; | |
// Make my data array. | |
$sharelinks = array( | |
'facebook' => $facebook, | |
'twitter' => $twitter, | |
'pinterest' => $pinlink | |
); | |
// Send it back. | |
return ! empty( $network ) && array_key_exists( $network, $sharelinks ) ? $sharelinks[ $network ] : $sharelinks; | |
} |
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 | |
/** | |
* Set the sharing buttons for posts. | |
* | |
* @param integer $post_id The post ID we want to use. | |
* @param boolean $display Whether to return or echo out the display. | |
* | |
* @return HTML The social buttons. | |
*/ | |
function rkv_social_share_build( $post_id = 0, $display = true ) { | |
// Bail without a post ID. | |
if ( empty( $post_id ) ) { | |
return; | |
} | |
// Get my link data array. | |
$sharelinks = rkv_get_social_share_links( $post_id ); | |
// Bail if it was empty for some reason. | |
if ( empty( $sharelinks ) ) { | |
return; | |
} | |
// Set an empty for the markup return. | |
$build = ''; | |
// Wrap the links in a div an unordered list class. | |
$build .= '<div class="post-share-links">'; | |
$build .= '<ul>'; | |
// Twitter link. | |
if ( ! empty( $sharelinks['twitter'] ) ) { | |
// Wrap the link. | |
$build .= '<li class="single-social-link single-social-twitter">'; | |
$build .= '<a href="' . esc_url( $sharelinks['twitter'] ) . '">Share on Twitter</a>'; | |
$build .= '</li>'; | |
} | |
// Facebook link. | |
if ( ! empty( $sharelinks['facebook'] ) ) { | |
// Wrap the link. | |
$build .= '<li class="single-social-link single-social-facebook">'; | |
$build .= '<a href="' . esc_url( $sharelinks['facebook'] ) . '">Share on Facebook</a>'; | |
$build .= '</li>'; | |
} | |
// Pinterest link. | |
if ( ! empty( $sharelinks['pinterest'] ) ) { | |
// Wrap the link. | |
$build .= '<li class="single-social-link single-social-pinterest">'; | |
$build .= '<a href="' . esc_url( $sharelinks['pinterest'] ) . '">Share on Pinterest</a>'; | |
$build .= '</li>'; | |
} | |
// Close the <ul> and the <div> | |
$build .= '</ul>'; | |
$build .= '</div>'; | |
// Filter to modify the HTML. | |
$build = apply_filters( 'rkv_social_links_build', $build ); | |
// Echo it if display was set to true | |
if ( ! empty( $display ) ) { | |
echo $build; | |
} | |
// Just return the markup. | |
return $build; | |
} |
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 | |
add_filter( 'the_content', 'rkv_social_share_display' ); | |
/** | |
* Filter the content and display the sharing buttons. | |
* | |
* @param HTML $content The original content about to be displayed. | |
* | |
* @return HTML $content The modified content about to return. | |
*/ | |
function rkv_social_share_display( $content ) { | |
// Optional filter to disable this all together. | |
if ( false === apply_filters( 'rkv_social_links_display', true, get_the_ID() ) ) { | |
return $content; | |
} | |
// Get my social display buttons. | |
$links = rkv_social_share_build( get_the_ID(), false ); | |
// Return either the links with the content, or just the content. | |
return ! empty( $links ) ? $content . $links : $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment