This is a more advanced tweet button function that has several advantages over the conventional code. I learned this from Gary’s Code. You should visit his site if you want to get a better understanding of why this is a better way to do this.
1. Include the custom function.
The first step is to open up your child theme’s functions.php file, and place the code below:
add_action( 'wp_print_scripts', 'child_add_tweet_button' );
/**
* Add tweet link and script.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/add-tweet-button/
*/
function child_add_tweet_button() {
// Only add this to anything not a page
if ( ! is_page() ) {
// Add a script to the bottom of the source
wp_enqueue_script( 'tweet-button', 'http://platform.twitter.com/widgets.js', array(), '', true );
// Filter in the required Twitter link for limited and unlimited content
add_filter( 'the_content', 'child_add_tweet_button_link' );
add_filter( 'the_content_limit', 'child_add_tweet_button_link' );
// Remove stripped link from excerpts.
add_filter( 'wp_trim_excerpt', 'child_remove_stripped_tweet_button' );
// Optionally add it in to excerpts properly (uncomment to use).
//add_filter( 'the_excerpt', 'child_add_tweet_button_link' );
}
}
/**
* Add tweet link.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/add-tweet-button/
*
* @param string $content HTML markup for post content
* @return string HTML markup for post content
*/
function child_add_tweet_button_link( $content ) {
global $post;
$query_string_parameters = array(
// URL of the page to share
'url' => get_permalink(),
// Screen name of the user to attribute the Tweet to
'via' => 'wallace_greg',
// Default Tweet text - here, the post title
'text' => get_the_title( $post->ID ),
// Related accounts that will be suggested to follow once tweet has been shared
'related' => '',
// Count box position - 'horizontal', 'vertical' or 'none'
'count' => 'vertical',
// The language for the Tweet Button - default is English (en)
'lang' => 'en',
// The URL to which your shared URL resolves to
'counturl' => get_permalink()
);
// Optionally use this if you have custom shortlinks set up. Uncomment to use.
//$query_string_parameters['url'] = wp_get_shortlink();
// Construct our URL to pass to Twitter - gets urlencoded here
$twitter_url = add_query_arg( $query_string_parameters, '//twitter.com/share' );
return '<a href="' . htmlspecialchars( $twitter_url ) . '" class="twitter-share-button">Tweet</a>' . $content;
}
/**
* Excerpt strip HTML, so the Tweet link just ends up as "Tweet" prefixed to the first word of the excerpt.
*
* Does mean that you can't start any article with the word Tweet, but this is an edge scenario.
*
* @author Gary Jones
* @link http://code.garyjones.co.uk/add-tweet-button/
*
* @param string $content Plain text
* @return string
*/
function child_remove_stripped_tweet_button( $content ) {
return preg_replace( '/^Tweet/', '', $content, 1 );
}
Be sure to replace the data-via=”xxxxxxx” with your Twitter username.
You can hook this anywhere you want by changing lines 17 and 18 to the desired area and removing the $content from line 37, and the . $content from line 70.
2. Include CSS in Your Style Sheet
The next step is to open up your child theme’s style.css file, and place the code below:
.twitter-share-button {
float: left;
margin: 0 10px 10px 0;
}
This .twitter-share-button class is automatically applied to the Tweet Button. Feel free to float the Tweet Button left or right, and modify the margin according to your needs.