WordPress pure code automatically generates tags for articles and automatically adds tag internal links

Pure code realizes the automatic generation of tags for WordPress articles, and the automatic addition of internal links to tags, which is much easier to use than the plug-ins WP Keyword Link and Ad Inserter. Every time I write an article, I organize tags by myself and add them manually, even if they are existing tags. For existing tags, it is best to be able to automatically add tags, otherwise Carrot Week will definitely collapse if it is done manually. In addition, after many WordPress sites have added the tag cloud function on the Internet, hyperlinks are also added to the text of the Tag tag in the article, which is equivalent to adding an internal link function to the website page, which is very friendly to website optimization and user experience. This method enables the WordPress site to automatically add Tag tags to articles for free, and automatically add links to these tags to become internal links.

Picture [1]-WordPress pure code automatically generates tags for articles and automatically adds tag internal links-Mijian.com

1. Realization of the function of automatically adding tags to articles

Just add the following code to the functions.php of your WordPress theme.

/* 自动为文章添加标签 */

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 ( strpos($post_content, $tag->name) !== false)

wp_set_post_tags( $post_id, $tag->name, true );

}

}

}

2. The article automatically adds a hyperlink to the tag content that appears in the article.

/* 自动为文章内的标签添加内链 */

$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接

$match_num_to = 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, '$'),__('点击了解更多关于[%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;

}

add_filter('the_content','tag_link',1);

The function of the above code is to automatically detect the content of the article and whether the label content appears when we publish/save/update the article. If it appears, it will automatically add an internal link to the tag in the article.
The specific effect is shown in this article!

Guess you like

Origin blog.csdn.net/wdsj_xh/article/details/130517252