Prefix WordPress Taxonomy Tags With Hashtag Symbol Like Twitter

Okay,

So this question has been quite old, and I had to genuinely struggle to find the solution, but now I know, How to do it. Here’s the trick.

It’s better for SEO and UX, to not include Hashtag in the tag itself, because when we open the taxonomy url, the browser would see the ‘#’ symbol and interprate it as html id="" and therefore search for that ID in the content.

Better approach is:
1.) Scan through post find all words starting with ‘# ‘
2.) Convert them to Tags, omit the Hash symbol, because as I explained Hash symbol can be messy.
3.) Use another function, in frontend post content, to convert each word beginning with # into a url, like say #Apple, will be saved as a Tag called Apple, but on frontend it would appear as #Apple, and clicking #Apple will take you to the url of taxonomy

        
function Milyin_Generate_Hashtags($my_post){ 
        $content = $my_post->post_content; 
        $ID = $my_post->ID;
        preg_match_all('/\B(\#[a-zA-Z]+\b)/', $content, $matches, PREG_PATTERN_ORDER); 
        if(isset($matches[1])){ 
                foreach($matches[1] as $matchKey){ 
                        wp_set_post_tags( $ID, $matchKey, true); 
                } 
        } 
} 
add_action('post_save', 'Milyin_Generate_Hashtags', 10 ,2 );


function Mentions($content) {
        $content = preg_replace('/([^a-zA-Z-_&])#([a-zA-Z_]+)/', "$1<a class=\"Milyin-Hashtags\"  href=\"https://milyin.com/hashtag/$2\" target=\"_blank\" >#$2</a>", $content);

        return $content;
}
add_filter('the_content', 'Mentions');