How can I add a hashtag symbol before each tag in my WordPress website? My intent is that WordPress tags like 'Apple' should get converted to #Apple'

I am the owner/developer of the site https://milyin.com/ and I want to make the things more social. As a result, I want my site to be more like twitter. I with the help of this answer was able to make a system with which authors of my site can use hashtags in actual content to convert them into actual tags. But the code doesn't have prefixed with a '#' symbol. You can see my posts for understanding better how it works... https://milyin.com/how-to-become-an-entrepreneur-with-no-money-and-experience/

I want the code to ensure that if in case there is already a Hashtag in the WordPress tag then it should not add another tag.

Here is the code with which I generate the tags from post content. These tags never include the hashtag symbol in the prefix.

function post_published_from_frontier($my_post) { 
    $content = $my_post->post_content;
    $ID = $my_post->ID; 
    preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
    if(isset($matches[1])) { 
        foreach($matches[1] as $matchKey) { 
            wp_set_post_tags( $ID, trim($matchKey), true);

add_action( 'frontier_post_post_save', post_published_from_frontier, 10 , 2 );

This code basically runs on the foundation of Frontier post plugin, and this answer helped me get the code. But somehow for me this code doesn't prefix them with a Hashtag symbol in the actual posts...

I tried to edit the wp_set_post_tags line and remove the trim() that didn't work, I tried '#'.$matchKey but that also didn't work...

i basically feel that the trouble is the wp_set_post_tags, so i edited that line from the the above code.I first tried this code,

wp_set_post_tags( $ID, $matchKey, true);

And then this

wp_set_post_tags( $ID, '#'.$matchKey, true);

But nothing worked...

  • Have you considered making all the actual tags without the hashtag symbol, and then making a child theme so that wherever tags are displayed, you manually add the hashtag there? That might help prevent problems WP may have with tags starting with a special character. – WebElaine Oct 16 at 15:27
  • See I need the # in the backend also.... I personally used a plugin that did it earlier, and a # in prefix was never a problem.... I deleted the plugin because of conflict with another plugin... Even now old posts of my site have tags with # in prefix... So please help with getting the trick done... – Aditya Agarwal Oct 16 at 15:36
  • 1
    _trim() that didn't work, I tried '#'.$matchKey but that also didn't work `` Can you show us the code? Please. Otherwise, there is zero probability somebody will answer you. – Max Yudin Oct 16 at 15:43
  • Sure i just updated the answer – Aditya Agarwal Oct 16 at 16:15
  • Anyone Would be able to help? I have included all necessary details.... – Aditya Agarwal Oct 17 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.