Skip to content

Auto tweet new WordPress posts with categories as hashtags in Zapier

Johnathon Williams
Johnathon Williams
3 min read
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.

automationcodejavascriptWordpresszapier

Related Posts

Members Public

How to use Zapier to extract all URLs from a text source

Here’s a nifty trick for pulling all of the URLs out of text with a Zapier javascript code action. Using this, you can extract URLs from any Zapier text input. For instance, you could use the Zapier email trigger to pull all of the URLs from an email message,

Members Public

Three tips for testing and debugging javascript in Zapier code actions

Zapier is bar none the handiest utility in existence for automating online workflows and connecting different online services. This was true even before the introduction of custom code actions, which allow you to drop in blocks of javascript and python code to manipulate data in your zaps, and it’s

Three tips for testing and debugging javascript in Zapier code actions
Members Public

Should you enable automatic updates in WordPress?

One of the headline features in the recent release of WordPress 5.5 was the ability to enable automatic plugin and theme updates. This follows the inclusion of automatic core updates in WP 3.7. On paper, the arguments for automatic updates are compelling. They’re the fastest and most

Should you enable automatic updates in WordPress?