0

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 '19 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 '19 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 '19 at 15:43
  • Sure i just updated the answer – Aditya Agarwal Oct 16 '19 at 16:15
  • Anyone Would be able to help? I have included all necessary details.... – Aditya Agarwal Oct 17 '19 at 1:08
0

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');


| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.