Jeremy Herve Avatar

That’s me. And my blog. In English and in French.


Jetpack: add hashtags to tweets sent by Publicize

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:

Publicize Options

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:

Tweet tags

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' );
Reactions on the Fediverse and on the ATmosphere

5 responses

  1. wobbledev Avatar

    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’ );

    }


  2. wobbledev Avatar

    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’ );

    }


  3. Raffaele Avatar
    Raffaele

    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.


    1. Jeremy Avatar

      That’s correct. However, this will only work for newly published posts.


  4. Raffaele Avatar
    Raffaele

    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.



Continuez votre lecture / Keep reading

Discover more posts about or look at some of the posts suggested below.

Jeremy Herve
Jeremy Herve

WordPress, TV Series, music, kids, and board games. I think that’s probably the best way to define me in a few words. 🙂

I work at Automattic where I lead a team building tools for bloggers and creators. I talk a lot about WordPress things, but also about all things open source in general.

I post in English and in French.

I live in Brittany, France, so you’ll also find me sharing pictures from our beautiful region from time to time.

1,241 posts
194 followers