Auto tweet new WordPress posts with categories as hashtags in Zapier

Auto tweet new WordPress posts with categories as hashtags in Zapier

Automatically sending tweets for new posts on your WordPress site is a simple task with Zapier. Recently, though, I wanted to add hashtags for each category assigned to the WordPress post and include that in the tweet as well. Can Zapier accommodate this?

Yes, it can, although it turned out to be a bit more difficult than I anticipated.

The problem

On the surface, it seemed like a simple problem. Each new WordPress post sent through Zapier brings along a comma-separated list of all categories assigned to it. Turning that into hashtags requires only four steps:

  • Remove any spaces from inside each category name
  • Transform each category name to lower case (not strictly necessary, but we might as well as hashtags aren’t case sensitive)
  • Remove the commas from the list
  • Append the number sign to each category to make it a hashtag

The solution

My first thought was to use Zapier’s built-in formatter actions to accomplish all of the above. The formatter allows us to split a comma separated list into individual line items, replace specific characters within each line item, lower case each line item, and even append characters to each line item.

However, try as I might, I couldn’t get consistent results with this approach. (Appending the # character to each line item is where I got stuck. I still think it’s possible with the formatter actions, but after 30 minutes or so I decided to try a different approach).

Fortunately, Zapier lets us drop blocks of javascript code as custom actions into our zaps. So I wrote this snippet to take care of all four steps in one action:

var busted = inputData.categories.split(',');
var hashray = busted.map(i => i.toLowerCase());
var hashray = hashray.map(i => '#' + i);
var hashray = hashray.map(i => i.replace(/\s/g, '')); 
var hashtags = hashray.join(" ");
output = { hashtags };

Here’s how it works:

  1. Take the comma separated list and split it into an array (what Zapier calls line items).
  2. Lower case each item in the array
  3. Add the hashtag symbol to each item
  4. Remove any spaces within each item
  5. Join each item in the array back into a single line of text, with each item separated by a space
  6. Return the finished output for use in the next action

To use the above code, create a zap triggered by a new post on your WordPress site.

Then add a code action and select Javascript as the language.

Next copy and paste the code snippet above into the code field, and fill out the input data fields as listed in the screenshot below.

Finally, add a Twitter action to push the title, link, and hashtags for your new post to Twitter.

And that’s it. Each new post on your WordPress site will now automatically send a tweet to the post, complete with hashtags for each of your post categories.

Are you struggling with automating parts of your business with Zapier? Get in touch to see how I can help.

Up Next:

Should you enable automatic updates in WordPress?

Should you enable automatic updates in WordPress?