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

相关推荐
在WooCommerce中添加“立即购买”按钮直接跳转到结算页面
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after…
日期:2024-10-19 点赞:0 阅读:15
实现 WordPress 自动更新所有文章的发布日期为当天的日期
//二开自动更新文章日期为当天发布。 //设置宝塔面板中的定时任务,通过访问带有?update_posts=run的链接触发文章更新.如:c…
日期:2024-09-22 点赞:0 阅读:58
WooCommerce订单列表显示购买产品
// 为 Legacy CPT-based 订单添加自定义列 add_filter('manage_edit-shop_order_colu…
日期:2024-09-20 点赞:0 阅读:115
WooCommerce Order 类的所有Get方法,以面向对象的方法获取订单数据
在开发WooCommerce插件或者与第三方系统交互时,我们需要获取 WooCommerce 订单的数据,WooCommerce的 WC_O…
日期:2024-09-20 点赞:0 阅读:86
为WooCommerce后台订单列表添加按自定义字段功能
  // 添加自定义列到订单列表 add_filter('manage_woocommerce_page_wc-orders_co…
日期:2024-09-20 点赞:0 阅读:72
添加删除 WooCommerce 数据列表中的数据列
转载:添加删除 WooCommerce 数据列表中的数据列 _WordPress智库 (wpzhiku.com) 在 WooCommerce…
日期:2024-09-20 点赞:0 阅读:93
发表评论
暂无评论

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