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.
Jetpack’s Publicize module allows you to automatically publish your posts to your favorite Social Networks like Twitter or Facebook.
You can customize the message that is posted to Social Networks thanks to the small input field right above the Publish button:

But what if you wanted to automatically add details to each Publicized post? Since both Twitter and Facebook support hashtags, you could append the post tags after the post title, like so:

To do so, add the following code to a functionality plugin:
function jeherve_publicize_hashtags() {
$post = get_post();
if ( ! empty( $post ) ) {
// Grab the tags of the post
$post_tags = get_the_tags( $post->ID );
// Append tags to custom message
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;
}
// Create our custom message
$custom_message = get_the_title() . ' ' . $hash_tags;
update_post_meta( $post->ID, '_wpas_mess', $custom_message );
}
}
}
// Save that message
function jeherve_cust_pub_message_save() {
add_action( 'save_post', 'jeherve_publicize_hashtags' );
}
add_action( 'publish_post', 'jeherve_cust_pub_message_save' );
3 replies on “Jetpack: add hashtags to tweets sent by Publicize”
Great post and really helpful and succinct.
You may find it useful to add a check to make sure Jetpack is active and the publicize module is also active, else do nothing.
// Check if JetPack is Installed and publicize is active, else do nothing.
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'publicize' ) ) {
function jeherve_publicize_hashtags() {
$post = get_post();
if ( ! empty( $post ) ) {
// Grab the tags of the post
$post_tags = get_the_tags( $post->ID );
// Append tags to custom message
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;
}
// Create our custom message
$custom_message = get_the_title() . ' ' . $hash_tags;
update_post_meta( $post->ID, '_wpas_mess', $custom_message );
}
}
}
// Save that message
function jeherve_cust_pub_message_save() {
add_action( 'save_post', 'jeherve_publicize_hashtags' );
}
add_action( 'publish_post', 'jeherve_cust_pub_message_save' );
}
Hey Jeremy, i add this code using the functionality plugin, as you suggested.
However no hashtags have been included in any tweets sent through Publicize.
Am i wrong or should this script automatically add any title word as #hashtag (together to the title i mean) as custom message on Publicize? What i have is just the title of the post, and its link.
Let me know if i misunderstood the purpose of this plugin or if i’m doing something wrong.
That’s correct. However, this will only work for newly published posts.