Wordpress教程 2023年06月24日
0 收藏 0 点赞 716 浏览 5361 个字

我们使用的是在当前主题 functions.php 文件中添加代码

<?php
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);'", "'&szlig;'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');

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2526.html

相关推荐
woocommerce 订单相关钩子
woocommerce_checkout_update_order_meta: 在订单元数据更新时触发。您可以在此钩子中更新订单的元数据,例…
日期:2024-03-15 点赞:0 阅读:213
最新Wp Rocket设置教程, 含插件下载+常见问题解决方法
最新Wp Rocket设置教程(超详细), 附GPL版本插件下载链接。图文并茂详细解释每一个设置的含义以及优缺点+兼容性排除,找到最合适的设…
日期:2024-02-24 点赞:0 阅读:227
WordPress破解版插件/主题(开心版, GPL, Nulled)安全吗?有啥缺点?如何获取?
解版插件产业链,安全与否,以及如何下载安全高质量破解版插件,避免下载有病毒主题/插件。 WordPress破解版插件/主题安全吗?这是绝大多…
日期:2024-02-24 点赞:0 阅读:199
终极WordPress速度优化教程, 秒开页面! 优化思路+方法+工具
终极WordPress速度优化教程,全面讲解影响速度的因素和原理。FCP, LCP, TTFB, TBT, CLS,Wp corn, Mar…
日期:2024-02-24 点赞:0 阅读:316
WordPress 的最佳 Cloudflare 设置(边缘涡轮增压)
无论您的 WordPress 主机有多快都无关紧要;您永远无法克服网络延迟。像 Cloudflare 这样的内容分发网络 (CDN) 通过将…
日期:2024-02-24 点赞:0 阅读:187
使用国外主题/插件后网站变很卡响应速度很慢的问题
变卡的原因 由于国内GFW的限制,有些资源被挡在海外无法加载,或者有些资源服务器延迟太高,导致网站加载速度缓慢,解决办法如下,二选一即可。 …
日期:2024-02-21 点赞:0 阅读:265
发表评论
暂无评论

还没有评论呢,快来抢沙发~