Jetpack: add hashtags to the twitter sharing button

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!

5 replies on “Jetpack: add hashtags to the twitter sharing button”

My bad, I thought you were commenting on the other post. :)

The trick here is that we can only prefill the sharing area for Twitter. Facebook and Google+ don’t offer options to add custom text to the sharing area.

I’m afraid you won’t be able to work around this limitation.