Search code examples
phpwordpresspluginstagsposts

Add A WordPress Taxonomy Tag for Hashtags in WordPress Content


WordPress taxonomies help in SEO, and they make sites with a large number of posts easy to sort. How can we convert any word starting with ' #' as a WordPress Tag automatically, whenever a post saves.

My site has 100s of posts, and authors Don't like to put tags. Using the system of hashtags can ensure that they enjoy putting tags.

For every WordPress post save, I want to find Words starting with ' #' for example 'An #Apple A Day' and convert #Apple to the default WordPress tag. Also, I want to do it for all post types.

I found a good explanation of how to do in comments, but because I'm not good at PHP, I couldn't do it with WordPress post's content.

Adding a taxonomy tag to wordpress post by writing it in comment with hashtag

I tried to do it with wp_insert_post_data, but it doesn't work.


Solution

  • first you must to build an plugin for wordpress that hook into the published post or an update post you can refer into this
    after that you can add tag whenever they find hastag on post_content and the code goes like this

    function post_published_notification( $ID, $post ) {
        $content = $post->post_content;
        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( 'publish_post', 'post_published_notification', 10, 2 );
    

    if you use frontier post maybe you can use this

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

    you can change the parameter of add_action priority refer to this and to change all of the hastag in the post into url you can use the code like this

    function old_wp_content( $content ) { 
        $content =  preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
        return $content;
    }
    add_filter( 'the_content', 'old_wp_content' ); 
    

    so if we combine all of the code into one plugin we can use it like this

    <?php
    function post_published_notification( $ID, $post ) {
        $content = $post->post_content;
        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( 'publish_post', 'post_published_notification', 10, 2 );
    
    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 );
    
    function old_wp_content( $content ) { 
        $content =  preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
        return $content;
    }
    add_filter( 'the_content', 'old_wp_content' );