Wordpress教程 2023年06月21日
0 收藏 0 点赞 630 浏览 4453 个字


// 在书籍分类文章发布时添加 save_post 钩子函数以触发自动添加wp-autotags插件标签的功能
add_action('save_post_book', 'auto_add_tags');

function auto_add_tags($post_ID) {
    $tags = get_option('wp_aatags_opts');
    $number = get_option('wp_aatags_aadnumber');
    $post_type = get_post_type($post_ID);
    if ($post_type != 'book') {
        return;
    }
    $auto_tag_slug = sanitize_title('自动');
    $draft_tag_slug = sanitize_title('草稿');
    wp_remove_object_terms($post_ID, array($auto_tag_slug, $draft_tag_slug), 'booktag');
    $original_tags = get_the_terms($post_ID, 'booktag');
    if (empty($original_tags)) {
        $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_set_post_terms($post_ID, $keywords, 'booktag', true);
        } else {
            wp_aatags_alts($post_ID, $post_title, $post_content);
        }
    }
}

// // 添加批量触发自动添加标签功能的页面
// function auto_add_tags_batch_page() {
//     add_submenu_page(
//         'edit.php?post_type=book',
//         '自动添加标签批处理',
//         '自动添加标签批处理',
//         'manage_options',
//         'auto-add-tags-batch',
//         'auto_add_tags_batch_callback'
//     );
// }
// add_action('admin_menu', 'auto_add_tags_batch_page');

// // 批处理页面回调函数
// function auto_add_tags_batch_callback() {
//     if (!current_user_can('manage_options')) {
//         return;
//     }
    
//     $total_posts = wp_count_posts('book')->publish; // 获取书籍总数
//     $batch_size = 50; // 每个批次处理的书籍数量
//     $current_batch = isset($_GET['batch']) ? absint($_GET['batch']) : 0; // 当前批次数
//     $offset = $current_batch * $batch_size; // 计算偏移量
//     $limit = $batch_size; // 每次查询的限制数量
    
//     $args = array(
//         'post_type' => 'book',
//         'post_status' => 'publish',
//         'posts_per_page' => $limit,
//         'offset' => $offset,
//     );
    
//     $query = new WP_Query($args);
    
//     echo '<div class="wrap">';
//     echo '<h1>自动添加标签批处理</h1>';
    
//     // 显示进度信息
//     echo '<p>总进度:' . ($offset + $query->post_count) . '/' . $total_posts . '</p>';
//     echo '<p>当前批次:' . ($current_batch + 1) . '</p>';
    
//     // 执行批量添加标签操作
//     if ($query->have_posts()) {
//         while ($query->have_posts()) {
//             $query->the_post();
//             auto_add_tags(get_the_ID());
//         }
//         wp_reset_postdata();
//     }
    
//     // 显示操作结果
//     echo '<p>本批次书籍已触发自动添加标签功能。</p>';
    
//     // 显示下一批次按钮
//     $next_batch = $current_batch + 1;
//     $next_url = admin_url('admin.php?page=auto-add-tags-batch&batch=' . $next_batch);
//     echo '<p><a href="' . $next_url . '" rel="external nofollow"  class="button">处理下一批书籍</a></p>';
    
//     echo '</div>';
// }


// 添加批量触发自动添加标签功能的页面
function auto_add_tags_batch_page() {
    add_submenu_page(
        'edit.php?post_type=book',
        '自动添加标签批处理',
        '自动添加标签批处理',
        'manage_options',
        'auto-add-tags-batch',
        'auto_add_tags_batch_callback'
    );
}
add_action('admin_menu', 'auto_add_tags_batch_page');

// 批处理页面回调函数
function auto_add_tags_batch_callback() {
    if (!current_user_can('manage_options')) {
        return;
    }
    
    $total_posts = wp_count_posts('book')->publish; // 获取书籍总数
    $batch_size = 50; // 每个批次处理的书籍数量
    $current_batch = isset($_GET['batch']) ? absint($_GET['batch']) : 0; // 当前批次数
    $offset = $current_batch * $batch_size; // 计算偏移量
    $limit = $batch_size; // 每次查询的限制数量
    
    $args = array(
        'post_type' => 'book',
        'post_status' => 'publish',
        'posts_per_page' => $limit,
        'offset' => $offset,
    );
    
    $query = new WP_Query($args);
    
    echo '<div class="wrap">';
    echo '<h1>自动添加标签批处理</h1>';
    
    // 显示进度信息
    echo '<p>总进度:' . ($offset + $query->post_count) . '/' . $total_posts . '</p>';
    echo '<p>当前批次:' . ($current_batch + 1) . '</p>';
    
    // 执行批量添加标签操作
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            auto_add_tags(get_the_ID());
        }
        wp_reset_postdata();
    }
    
    if (($offset + $query->post_count) < $total_posts) {
        // 自动触发下一批次
        $next_batch = $current_batch + 1;
        $next_url = admin_url('admin.php?page=auto-add-tags-batch&batch=' . $next_batch);
        echo '<script>setTimeout(function() { window.location.href = "' . $next_url . '"; }, 2000);</script>';
    } else {
        // 所有书籍已处理完成
        echo '<p>所有书籍已触发自动添加标签功能。</p>';
    }
    
    echo '</div>';
}




// 当保存书籍自定义字段时,本地化外链图片
add_action('save_post_book', 'localize_book_thumbnail');

function localize_book_thumbnail($post_id) {
    // 获取字段的值
    $thumbnail_url = isset($_POST['book_post_meta']['_thumbnail']) ? $_POST['book_post_meta']['_thumbnail'] : '';

    // 检查是否是外链图片
    if (filter_var($thumbnail_url, FILTER_VALIDATE_URL) !== false) {
        // 获取默认的上传路径
        $upload_dir = wp_upload_dir();

        // 生成本地文件路径
        $file_path = $upload_dir['basedir'] . '/book-thumbnails/';
        wp_mkdir_p($file_path); // 创建文件夹(如果不存在)

        // 获取外链图片文件名
        $file_name = basename($thumbnail_url);

        // 生成本地文件路径和URL
        $local_file_path = $file_path . $file_name;
        $local_file_url = $upload_dir['baseurl'] . '/book-thumbnails/' . $file_name;

        // 下载并保存外链图片到本地
        if (file_put_contents($local_file_path, file_get_contents($thumbnail_url))) {
            // 更新自定义字段的值为本地URL
            $_POST['book_post_meta']['_thumbnail'] = $local_file_url;
        }
    }
}







 

微信扫一扫

支付宝扫一扫

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

相关推荐
WordPress去除/禁止后台侧栏插件-主题-版本更新提示
今天使用了张戈修改的nginx-help插件修改版,官方版并无自动重启功能,因此不能更新。但是这WordPress后台一直有更新提示,这可真…
日期:2024-12-30 点赞:0 阅读:44
WordPress网站js防扒代码-禁止右键/F12/调试自动关闭窗口
网站防扒老生常谈了,今天老白博客@老白也给大家分享一下从网上看到的“WordPress网站防扒代码/禁止F12调试教程-以7b2主题为例” …
日期:2024-12-30 点赞:0 阅读:59
WordPress优化加速之导航菜单栏缓存Menu Cache
今天老白博客@老白给大家分享又一个WordPress优化加速教程:“导航菜单栏缓存WordPress插件Menu Cache”。我们都知道数…
日期:2024-12-30 点赞:0 阅读:45
2024基于网站性能测试的WordPress优化指南
老白博客WordPress优化高级教程:在如今5G遍地走,千兆多如狗的高速网络时代,如果你访问网站,还出现转圈圈的情况,那么你十有八九是要关…
日期:2024-12-30 点赞:0 阅读:50
WP网站用户行为监测回放插件nicen-replay
在数字营销的世界里,了解用户行为是提升用户体验和转化率的关键。老白推荐一款炫酷的WordPress插件:nicen-replay 。它能够让…
日期:2024-12-30 点赞:0 阅读:48
2024新WordPress缓存插件atec Cache APCu
WordPress插件atec Cache APCu 是一款2024年才投入使用的的高效缓存插件,与其他内存缓存选项(如 Redis 和 M…
日期:2024-12-30 点赞:0 阅读:66
发表评论
暂无评论

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