Wordpress教程 2023年06月24日
0 收藏 0 点赞 854 浏览 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

相关推荐
WordPress标签按拼音字母层级排序教程
此教程实现标签按拼音字母(A、B、C 等字母)层级排序,并在页面上添加一个侧边竖向字母导航 <?php /* Template Nam…
日期:2024-11-07 点赞:0 阅读:50
wordpress网站编辑器启隐藏的编辑器功能
如果开启WordPress隐藏的编辑器功能就够你用了,何必安装插件呢。 如何开启隐藏的编辑器功能 WordPress默认不支持TinyMCE…
日期:2024-10-29 点赞:0 阅读:61
在WooCommerce中添加“立即购买”按钮直接跳转到结算页面
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after…
日期:2024-10-19 点赞:0 阅读:78
实现 WordPress 自动更新所有文章的发布日期为当天的日期
//二开自动更新文章日期为当天发布。 //设置宝塔面板中的定时任务,通过访问带有?update_posts=run的链接触发文章更新.如:c…
日期:2024-09-22 点赞:0 阅读:103
WooCommerce订单列表显示购买产品
// 为 Legacy CPT-based 订单添加自定义列 add_filter('manage_edit-shop_order_colu…
日期:2024-09-20 点赞:0 阅读:197
WooCommerce Order 类的所有Get方法,以面向对象的方法获取订单数据
在开发WooCommerce插件或者与第三方系统交互时,我们需要获取 WooCommerce 订单的数据,WooCommerce的 WC_O…
日期:2024-09-20 点赞:0 阅读:202
发表评论
暂无评论

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