Back to Top

How to Extend WordPress Website Functionality Using Code Snippets?

Code-snippets

WordPress is the most used content management system, but nowadays WordPress is used for developing any kind of site like Restaurant, Job Search, shopping cart etc.

WordPress provides tags system which is used for find matched post or content via keywords. Well, sometimes to add tags manually for every post or content takes much time to find matched keywords and can be a major headache.

Here In the following function, I have explained that to fetch terms of database and check terms are exists into a description of a post when you save a post in WordPress. If term found in the description, will add into the array and consider keyword of the post and add as a tag.

Must Read: Post via e-mail in WordPress


add_action( 'save_post', 'auto_add_post_tags' );

function auto_add_post_tags(){
     global $post; 
    $post_info =get_post($post->ID);
    $post_id =$post->ID;
    $global_tags = array();
    $terms = get_terms(array('post_tag'), array('hide_empty' => false));
    if($terms)
    {    $posted['tags'] = '';
        foreach($terms as $term)
        {
            $tag_found = strpos($post_info->post_content, $term->name);
                
                if($tag_found)
                {
                    $posted['tags'].= ','.$term->name;
                }
        }
    }         
    if ($posted['tags'] !='') 
            {        
                $explode = explode(',', $posted['tags']);        
                foreach($explode as $exp)
                {
                    $exp = trim($exp);
                    if($exp != '')
                    {
                        $term_exists = term_exists($exp, 'post_tag');                        
                        if($term_exists && is_array($term_exists))
                        {
                            $post_into_tags[] = get_term_by( 'id', (int)$term_exists['term_id'], 'post_tag')->slug;
                            $term_details = get_term_by( 'id', (int)$term_exists['term_id'], 'post_tag');                            
                        }
                        else
                        {
                            $term = wp_insert_term($exp, 'post_tag');
                            $post_into_tags[] = get_term_by( 'id', (int)$term['term_id'], 'post_tag')->slug;
                            $term_details = get_term_by( 'id', (int)$term['term_id'], 'post_tag');
                        }    
                    }
                }
            }
        wp_set_object_terms($post_id, $post_into_tags, 'post_tag');
}

Above function will insert term into the database and then assign that term to the particular post.You can place code into theme’s functions.php file.

Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How To Turn On Output Buffering

Posted on 13 years ago

Bhumi

What are Traits in PHP

Posted on 10 years ago

Bhumi

How To Create a Temporary File in PHP

Posted on 13 years ago

Bhumi