A few days ago I explained how to add hashtags to the tweets sent out by Jetpack Publicize. But what about the tweets your read can send using the Jetpack Sharing buttons at the bottom of your posts?
Here is how to get tags from your posts, and add them as hashtags to the twitter sharing button. As always, you’ll want to paste that code in a functionality plugin.
function jeherve_custom_sharing_title() {
$post = get_post();
if ( empty( $post ) ) {
return;
} else {
// Create sharing title
$sharing_title = get_the_title( $post->ID );
// Get the tags
$post_tags = get_the_tags( $post->ID );
if ( ! empty( $post_tags ) ) {
// Create list of tags with hashtags in front of them
$hash_tags = '';
foreach( $post_tags as $tag ) {
$hash_tags .= ' #' . $tag->name;
}
// Add tags to the title
$sharing_title .= $hash_tags;
}
return $sharing_title;
}
}
add_filter( 'sharing_title', 'jeherve_custom_sharing_title', 10, 3 );
Hat tip to Ryan for the idea!