Do you use the Jetpack plugin and its Social feature, or the Jetpack Social standalone plugin? Are you active on Mastodon? This little tip is for you!
8 years ago, hashtags were all the rage. If you wanted to reach more folks on Twitter, adding some hashtags to your tweets was a good idea. 8 years later, Twitter is dying but hashtags are still around, and quite useful on Mastodon!
In a discussion with @bradlinder and @voxpelli, we talked about automatically adding your post’s tags to the end of the messages that get posted on Mastodon by Jetpack Social.
That is something you can already do when writing your posts in your WordPress editor, before to hit Publish:

However, someone may not want to have to manually add all their tags every time they write a new post. Yes, manual until it hurts, but here we can automate this with a small code snippet!
To automatically add your post’s tags to the end of messages on Mastodon, you’ll need to add the following PHP code to your theme’s functions.php
file, or use a functionality plugin to add the code snippet:
/**
* Automatically add any tags you may have added to your post
* to the end of each message shared by Jetpack Social
* on your connected Social Networks.
*
* @see https://jeremy.hu/jetpack-hashtags-posts-shared-mastodon/
*/
/**
* Append a post's tags to saved Jetpack Social Meta data.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*
* @return void
*/
function jeherve_jetpack_social_metadata_hashtags( $post_id, $post ) {
// Grab the tags of the post.
$post_tags = get_the_tags( $post_id );
// Append tags to custom message.
if ( ! empty( $post_tags ) ) {
/*
* Let's build an array of tags.
* We'll massage the output a bit to add hashtags in front of each tag.
* We'll capitalize the first letter of each word, for better accessibility.
*/
$post_tags_array = array_map(
function ( $tag ) {
// Camel case the tag name and remove spaces as well as apostrophes.
$tag = preg_replace( '/\s+|\'|-/', '', ucwords( $tag->name ) );
// Return with a '#' prepended.
return '#' . $tag;
},
$post_tags
);
// Build a string of the tags.
$hash_tags = implode( ', ', $post_tags_array );
/*
* Get the existing custom message if it exists.
* If not, our custom message will be the post title.
*/
$custom_message = get_post_meta( $post_id, '_wpas_mess', true );
if ( empty( $custom_message ) ) {
$custom_message = get_the_title( $post );
}
// Append our generated hashtags to the custom message.
$custom_message .= ' ' . sanitize_text_field( $hash_tags );
// Update post meta with the new message.
update_post_meta( $post_id, '_wpas_mess', $custom_message );
}
}
add_action( 'publish_post', 'jeherve_jetpack_social_metadata_hashtags', 10, 2 );
That’s all there is to it! Give it a try, and let me know how it went for you!

Side-note: you can also automate adding hashtags to posts shared by your visitors using the Mastodon sharing button.
4 replies on “Jetpack: automatically add hashtags to posts shared to Mastodon”
@jeremy Very nice 👏 This is what I love about the OSS community!
[…] Update, August 3, 2023: Twitter is dying so this method isn’t useful for Twitter anymore. It does, however, still work for other social networks you may have connected to your site via Jetpack. Check this post for an updated code snippet for Mastodon. […]
@jeremy @bradlinder @voxpelli Can you explain a bit more? IE, on your post above, did code add those hashtags? Does it remove tags (that you added) in the text and add them back at the end with #?
Once you add the code snippet to your site, you do not have to think about hashtags anymore. All you have to think about is adding tags to your post in the WordPress editor, as you write your post. If you do that, when you hit the Publish button a toot will be published on your Mastodon profile with the post title and each of the tags you had added to your post in WordPress.
If you want to add your own custom message and hashtags manually in the WordPress editor as you’re writing your post, you still can. That custom message will be used and the extra tags from your post will be appended to that custom message.