WordPress implements automatic inline method for adding article tags

How to automatically add internal links to tags in articles using pure WordPress code :

Place the following code in your theme ’s functions.phpThe article originates fromSinsTu NI-https://www.sinstu.com/archives/72.html

//Automatically convert TAG to internal link$match_num_from = 1 ; // How many times a TAG tag appears before adding a link$match_num_to = 1 ; // How many times to add links to the same tagadd_filter('the_content','tag_link',1);function tag_sort($a, $b){if ( $a->name == $b->name ) return 0;return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;}function tag_link($content){global $match_num_from,$match_num_to;$posttags = get_the_tags();if ($posttags) {usort($posttags, "tag_sort");foreach($posttags as $tag) {$link = get_tag_link($tag->term_id);$keyword = $tag->name;$cleankeyword = stripslashes($keyword);$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";$url .= ' target="_blank"';$url .= ">".addcslashes($cleankeyword, '$')."</a>";$limit = rand($match_num_from,$match_num_to);$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);$cleankeyword = preg_quote($cleankeyword,'\'');$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;$content = preg_replace($regEx,$url,$content,$limit);$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);}}return $content;}

The function of the above code is to automatically detect the content of the article and whether tag content appears when we publish/save/update the article. If it appears, an internal link will be automatically added to the tag in the article.The article originates fromSinsTu NI-https://www.sinstu.com/archives/72.html

/* Automatically add tags to articles */add_action('save_post', 'auto_add_tags');function auto_add_tags(){    $tags = get_tags( array('hide_empty' => false) );    $post_id = get_the_ID();    $post_content = get_post($post_id)->post_content;    if ($tags) {        foreach ( $tags as $tag ) {           // If tags that have been used appear in the article content, automatically add these tags            if ( strpos($post_content, $tag->name) !== false)                wp_set_post_tags( $post_id, $tag->name, true );        }    }}

The function of the above code is to automatically detect whether the content in the article appears when we publish/save/update the article. These tags will be automatically added to the article if they appear.The article originates fromSinsTu NI-https://www.sinstu.com/archives/72.html The article originates fromSinsTu NI-https://www.sinstu.com/archives/72.html

 
admin
  • by admin Published on 2024-04-1117:42:12
  • Please make sure to keep the link to this article when reprinting:https://www.sinstu.com/archives/72.html

Comment