Categories
How It Is

Hashtag your Jetpack with Taxonomies

A different way to customize your hashtags.

So maybe you read my post about hash tagging Jetpack Publisher posts with a complex check of “Is this tag also a custom post type?” and you thought “Mika, that’s awesome. But I don’t have a database of shows!”

To my reporter friend who lamented this to me, I have a solution.

The Real Problem The Real Problem

I think most of the problem with Jetpack’s custom Publicize message is that it’s (rightly) hidden by default. Most people don’t need it. But those that do want to see it so we remember “and do this…”

Publicize Settings on your post sidebar

And let’s be honest, this visibility issue is going to be worse when we move to Gutenberg.

Again, I don’t think Jetpack is in the wrong here. The majority of users don’t need to be so aggro about their messages. There are a lot of WordPress sites that we would classify as ‘smaller’ sites. And those who aren’t tend to need very bespoke/custom solutions, which is the problem.

My solution works for me because it’s easily automated and checkable. We’re very pedantic about tags (seriously, Tracy went through all our tags and cleaned them up), we have a great system to match tag to show, and, most significantly, we know that our social media engagement relies on not just content, but the appropriate hashtags. That is, the shows we’re talking about.

This means there are two types of ways to do this:

1) Make all your tags (and/or categories) into your hashtags
2) Make a custom taxonomy for your hashtags

Since using all the tags might be a bit much, I went with option 2.

Top ↑

The Code The Code

<?php
/*
 * Jetpack tweaks
 * @version 1.0
 * @package mu-plugins
 */

class HalfElf_Jetpack {

	public function __construct() {
		add_action( 'publish_post', array( $this, 'custom_message_save' ) );
		add_action( 'init', array( $this, 'register_taxonomy_hashtag' ) );
	}

	public function register_taxonomy_hashtag() {

		//parameters for the new taxonomy
		$arguments = array(
			'label'                 => 'Hashtags',
			'hierarchical'          => false,
			'public'                => false,
			'show_ui'               => true,
			'update_count_callback' => '_update_post_term_count',
			'rewrite'               => false,
		);

		register_taxonomy( 'flf_hashtags', 'post', $arguments );
	}

	public function publicize_hashtags() {
		$post      = get_post();
		$hash_tags = '';

		// If the post isn't empty AND it's a post (not a page etc), let's go!
		if ( ! empty( $post ) && 'post' === get_post_type( $post->ID ) ) {

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

			// First let's add the hashtags
			$post_tags = get_the_terms( $post->ID, 'flf_hashtags' );
			if ( ! empty( $post_tags ) ) {
				// Create list of tags with hashtags in front of them
				foreach ( $post_tags as $tag ) {
					// Change tag from this-name to thisname and slap a hashtag on it.
					$tag_name   = str_replace( '-', '', $tag->slug );
					$hash_tags .= ' #' . $tag_name;
				}
			}

			// Next we add a category in specific situations.
			$post_cats = get_the_category( $post->ID );
			if ( ! empty( $post_cats ) ) {
				// Create list of tags with hashtags in front of them
				foreach ( $post_cats as $cat ) {
					if ( 'MAINCAT' === $cat->slug ) {
						// Change slug from this-name to thisname and slap a hashtag on it.
						$cat_name   = str_replace( '-', '', $cat->slug );
						$hash_tags .= ' #' . $cat_name;
					}
				}
			}
		}

		// Loop back. If there are hashtags, we add them.
		if ( '' !== $hash_tags ) {
			// Create our custom message
			$custom_message = 'New post! ' . get_the_title() . $hash_tags;
			update_post_meta( $post->ID, '_wpas_mess', $custom_message );
		}
	}

	// Save that message
	public function custom_message_save() {
		add_action( 'save_post', array( $this, 'publicize_hashtags' ) );
	}

}

new HalfElf_Jetpack();

Top ↑

A Little Explanation A Little Explanation

You may notice I added in a bit that looks for a specific category:

if ( 'MAINCAT' === $cat->slug ) { ... }

The reason here is that on the specific site I wrote this for, they have four (yes four!) categories:

1) Announcements
2) News
3) Fandom (actually named FOR the fandom)
4) Miscellaneous

They wanted item #3 to be a tag, so it would always #JanelleMonae (for example). For them I did a super basic ‘if the fandom, then the hashtag’ but a slightly more common situation would be someone having a category for ‘Fandoms’ and then subcategories our fandom. For that you’ll want something like this:

foreach ( $post_cats as $cat ) {
	$cat_mom = $cat->category_parent;
	if( $cat_mom > 0 && 'fandoms' === $cat_mom->slug ) {
		// Change slug from this-name to thisname and slap a hashtag on it.
		$cat_name   = str_replace( '-', '', $cat->slug );
		$hash_tags .= ' #' . $cat_name;
	}
}

Enjoy your hash tagging!

1 reply on “Hashtag your Jetpack with Taxonomies”

Comments are closed.