Hashtags in PHP
How to generate hashtags from slugs and automatically link hashtags from text.
Working on a project today I had a few tasks to accomplish in PHP, specifically WordPress which I love to use for side-project sites due to its flexibility.
Even though I’m not a fan of its code quality, I do admire how easy it is to do basically anything and customize anything with the use of filters and actions.
So first up I needed to convert a plain text string into a hashtag. For example, “Make America Great Again” should become #makeamericagreatagain.
Next up, if we wanted to automatically hyperlink any hashtags in a paragraph to Twitter searches.
Two simple, but effective functions.

Damian Hodgkiss
Senior Staff Engineer at Sumo Group, leading development of AppSumo marketplace. Technical solopreneur with 25+ years of experience building SaaS products.