Getting WordPress posting to Twitter with hashtags

In trying to publicise some of my articles in this blog a bit wider afield I recently opened a twitter account. However, in order to reach a wider audience on twitter you need to use hashtags. Unfortunately WordPress’s excellent Jetpack extension, whilst allowing you to post to twitter and other social networks, doesn’t automatically include hashtags in your posts. There have been a few attempts to add this functionality as an extension in this thread however they are all not very well coded and don’t work properly. Here is what I am now using on this blog:

// Create our custom Publicize message
function jeherve_cust_pub_message() {
	$post = get_post();

	if ( !$post )
		return;

	$categories = get_the_category( $post->ID );

	if ( !$categories )
		return;

	$msg = $post->post_title;
	// No need to add a URL as that is a separate part of the message.
	foreach($categories as $category)
		$msg .= " #" . str_replace(' ', '', $category->cat_name);

	update_post_meta( $post->ID, '_wpas_mess', $msg );
}

// Save that message
function jeherve_cust_pub_message_save() {
	add_action( 'save_post', 'jeherve_cust_pub_message', 21 );
}
add_action( 'publish_post', 'jeherve_cust_pub_message_save' );

Leave a Reply

Your email address will not be published. Required fields are marked *