近日,有朋友说,每次发文章都要手动去设置内容页关键词(Tags关键词)好麻烦啊,能不能让它自动生成呢?
这个主意倒是挺好的,我就在想,既然要自动生成,那就要给程序一个依据,从而去判断标准和执行。
朋友说,我找了好久也没有找到方法。。。。
我说,没有那么难吧,无非就几种思路:
- 通过文章页标题来自动拆分为关键词
- 通过文章页标题和内容当中的词语出现的次数来自动提取关键词
- 通过文章页内容与网站原有的关键词词库对比,如果有相同的关键词就提取一下。
他却说还是没有找到。。。
如是,我就查询了一下,找到了如下的几种办法:
文章目录
1,通过标题来自动拆分关键词:
找到wp-includes/general-template.php文件, 原本为:
/**
* Displays title tag with content.
*
* @ignore
* @since 4.1.0
* @since 4.4.0 Improved title output replaced `wp_title()`.
* @access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
替换为:
/** * Displays title tag with content. * * @ignore * @since 4.1.0 * @since 4.4.0 Improved title output replaced `wp_title()`. * @access private */ function _wp_render_title_tag() { if ( ! current_theme_supports( 'title-tag' ) ) { return; } // 文章标题 $title = wp_get_document_title(); // 先展示<title/> echo '<title>' . $title . '</title>' . "\n"; // 标题格式会带有站点信息, 这里将其截去. // 大家在使用的时候需要根据自己的情况修改`-19`这个参数 $title = trim(substr($title, 0, -19)); // 中文标题中包含的英文一般都是完整的关键词. // 而pullword这个分词工具对英文的效果不太好, 这里先将英文关键词提取出来 preg_match_all('/[a-zA-Z]+[0-9a-zA-Z]+/', $title, $matches); $engKeywords = join(",", $matches[0]); // 调用pullword api, param1为最低概率阈值, param2为debug模式开关(0=关闭) $url='http://api.pullword.com/get.php?source='. urlencode($title) .'¶m1=0.9¶m2=0'; $html = trim(file_get_contents($url)); if ($html == "error"){ echo '<meta name="keywords" content="' . $engKeywords. '" />'; return; } // 将分词得到的关键词拼接到tag中 $keywords = str_replace("\n",",",$html); if ($keywords != ""){ if ($engKeywords != ""){ $keywords = $keywords . "," . $engKeywords; } } // 展示keyword <meta> echo '<meta name="keywords" content="' . $keywords . '" />'; }
2,通过文章页标题和内容当中的词语出现的次数来自动提取关键词:
<?php
/*
Plugin Name: WP AutoTags
Description:英文:Often publish articles and updates people loves forgets to set tags when editing an article, the article automatically add keywords tag. tag extraction based on TF-IDF implementation. Built-in TF-IDF interface does not work when you try to find an existing tag in WordPress. If found, these markers are added to the post automatically each time you save the post.
Version: 0.1.14
License: GPLv2
*/
function wp_aatags_html2text($ep) {
$search = array("'<script[^>]*?>.*?</script>'si", "'<[\/\!]*?[^<>]*?>'si", "'([\r\n])[\s]+'", "'&(quot|#34|#034|#x22);'i", "'&(amp|#38|#038|#x26);'i", "'&(lt|#60|#060|#x3c);'i", "'&(gt|#62|#062|#x3e);'i", "'&(nbsp|#160|#xa0);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&(reg|#174);'i", "'&(deg|#176);'i", "'&(#39|#039|#x27);'", "'&(euro|#8364);'i", "'&a(uml|UML);'", "'&o(uml|UML);'", "'&u(uml|UML);'", "'&A(uml|UML);'", "'&O(uml|UML);'", "'&U(uml|UML);'", "'ß'i");
$replace = array("", "", "\\1", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), chr(174), chr(176), chr(39), chr(128), "ä", "ö", "ü", "Ä", "Ö", "Ü", "ß");
return preg_replace($search, $replace, $ep);
}
function wp_aatags_sanitize($taglist) {
$special_chars = array('?', '、', '。', '“', '”', '《', '》', '!', ',', ':', '?', '.', '[', ']', '/', '\\', '\=', '<', '>', ':', ';', '\'', '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', chr(0));
/**
* Filter the list of characters to remove from a taglist.
* @param array $special_chars Characters to remove.
*/
$taglist = preg_replace("#\x{00a0}#siu", ' ', $taglist);
$taglist = str_replace($special_chars, '', $taglist);
$taglist = str_replace(array('%20', '+'), '-', $taglist);
$taglist = preg_replace('/[\d]+/', '', $taglist);
$taglist = preg_replace('/[\r\n\t -]+/', '-', $taglist);
$taglist = trim($taglist, ',-_');
return $taglist;
}
function wp_aatags_keycontents($keys, $num) {
$request = wp_remote_request('https://cws.9sep.org/extract/json', array('method' => 'POST', 'timeout' => 9, 'body' => array('text' => $keys, 'topk' => $num)));
if (wp_remote_retrieve_response_code($request) != 200) {
return 'rEr';
} else {
return wp_remote_retrieve_body($request);
}
}
function wp_aatags_kwsiconv($kws) {
return wp_aatags_sanitize(@json_decode($kws, true)['kws']);
}
function wp_aatags_alts($post_ID, $post_title, $post_content) {
$tags = get_tags(array('hide_empty' => false));
$tagx = get_option('wp_aatags_opts');
$number = get_option('wp_aatags_aadnumber');
switch ($tagx) {
case 3:
$d = strtolower($post_title);
break;
case 2:
$d = strtolower(wp_trim_words($post_content, 999, '') . ' ' . $post_title);
break;
default:
$d = strtolower(wp_trim_words($post_content, 333, '') . ' ' . $post_title);
break;
}
if ($tags) {
$i = 0;
foreach ($tags as $tag) {
if (strpos($d, strtolower($tag->name)) !== false) {
wp_set_post_tags($post_ID, $tag->name, true);
$i++;
}
if ($i == $number) {
break;
}
}
}
}
function wp_aatags_run($post_ID) {
$tags = get_option('wp_aatags_opts');
$number = get_option('wp_aatags_aadnumber');
global $wpdb;
if (get_post($post_ID)->post_type == 'post' && !wp_is_post_revision($post_ID) && !get_the_tags($post_ID)) {
$post_title = get_post($post_ID)->post_title;
$post_content = get_post($post_ID)->post_content;
switch ($tags) {
case 3:
$requix = strtolower($post_title . ' ' . wp_trim_words($post_content, 333, ''));
break;
case 2:
$requix = strtolower($post_title . ' ' . wp_trim_words($post_content, 999, ''));
break;
default:
$requix = strtolower($post_title);
break;
}
$body = wp_aatags_keycontents(wp_aatags_html2text($requix), $number);
if ($body != 'rEr') {
$keywords = wp_aatags_kwsiconv($body);
wp_add_post_tags($post_ID, $keywords);
} else {
wp_aatags_alts($post_ID, $post_title, $post_content);
}
}
}
function wp_aatags_admin_init() {
if (get_bloginfo('language') == 'zh-CN' || get_bloginfo('language') == 'zh-TW') {
$wp_aatags_setting = 'WP AutoTags处理范围';
$wp_aatags_number = '自动标签数量';
} else {
$wp_aatags_setting = 'WP AutoTags Matching range';
$wp_aatags_number = 'Automatic Tags number';
}
add_settings_field('wp_aatags_opts', $wp_aatags_setting, 'wp_aatags_setting', 'writing', 'default');
add_settings_field('wp_aatags_aadnumber', $wp_aatags_number, 'wp_aatags_aadnumber', 'writing', 'default');
register_setting('writing', 'wp_aatags_opts');
register_setting('writing', 'wp_aatags_aadnumber');
}
function wp_aatags_install($obj) {
add_option('wp_aatags_opts', 3);
add_option('wp_aatags_aadnumber', 3);
}
function wp_aatags_uninstall() {
delete_option('wp_aatags_opts');
delete_option('wp_aatags_aadnumber');
remove_action('admin_init', 'wp_aatags_admin_init');
}
function wp_aatags_setting() {
$wp_aatags_opts = get_option('wp_aatags_opts');
?>
<select name="wp_aatags_opts">
<option value="1" <?php selected('1', $wp_aatags_opts);?>><?php if (get_bloginfo('language') == 'zh-CN' || get_bloginfo('language') == 'zh-TW'): ?>标题+正文前333字<?php else: ?>Posts Title&Content before 333.<?php endif;?></option>
<option value="2" <?php selected('2', $wp_aatags_opts);?>><?php if (get_bloginfo('language') == 'zh-CN' || get_bloginfo('language') == 'zh-TW'): ?>文章内容前999字<?php else: ?>Only Posts Content before 999.<?php endif;?></option>
<option value="3" <?php selected('3', $wp_aatags_opts);?>><?php if (get_bloginfo('language') == 'zh-CN' || get_bloginfo('language') == 'zh-TW'): ?>仅匹配文章标题<?php else: ?>Only Posts Title<?php endif;?></option>
</select>
<?php
}
function wp_aatags_aadnumber() {
$wp_aatags_aadnumber = get_option('wp_aatags_aadnumber');
?>
<p><label><input name="wp_aatags_aadnumber" type="radio" value="3" <?php checked('3', $wp_aatags_aadnumber);?>>3 </label><label><input name="wp_aatags_aadnumber" type="radio" value="5" <?php checked('5', $wp_aatags_aadnumber);?>> 5 </label><label><input name="wp_aatags_aadnumber" type="radio" value="9" <?php checked('9', $wp_aatags_aadnumber);?>> 9 </label><label><input name="wp_aatags_aadnumber" type="radio" value="15" <?php checked('15', $wp_aatags_aadnumber);?>> 15</label></p>
<?php
}
register_activation_hook(__FILE__, 'wp_aatags_install');
register_deactivation_hook(__FILE__, 'wp_aatags_uninstall');
add_action('admin_init', 'wp_aatags_admin_init');
add_action('publish_post', 'wp_aatags_run');
add_action('edit_post', 'wp_aatags_run');
3,通过文章页内容与网站原有的关键词词库对比,如果有相同的关键词就提取一下:
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 );}
}
}
评论