这款插件为WordPress后台添加了强大的批量操作功能,包括一键发布/更新/草稿等实用功能,极大提升文章管理效率。

功能概述
本插件提供以下核心功能:
- 一键发布所有草稿:批量将草稿状态文章转为发布状态
- 一键更新发布时间:将所有已发布文章的发布时间更新为当前时间
- 一键移动到草稿箱:批量将已发布文章转为草稿状态
- 标题批量编辑:在文章标题前后批量添加指定内容
安装部署
将以下代码添加到主题的func.php文件中:
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一下,记得加上php头,要不然会保存,将下面的代码里面放里面,我以子比主题来演示的,其他的主题自行放!
// 功能:1.一键发布所有草稿 2.一键更新发布时间 3.一键移动到草稿箱 4.一键在标题前后添加指定名称
/**
* 在文章列表页添加所有批量操作按钮(包括新的标题添加按钮)
*/
function enhanced_admin_post_bulk_buttons() {
global $current_screen;
// 仅在文章列表页面 (edit-post) 显示
if ( 'edit-post' == $current_screen->id ) {
// 获取各状态文章数量用于显示
$publish_count = get_posts_count('publish');
$draft_count = get_posts_count('draft');
?>
<style>
.bulk-action-section {
margin: 12px 0;
padding: 12px;
background: #fff;
border: 1px solid #c3c4c7;
box-shadow: 0 1px 1px rgba(0,0,0,.04);
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 15px;
}
.bulk-action-group {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
padding-right: 15px;
border-right: 1px solid #ddd;
}
.bulk-action-group:last-child {
border-right: none;
}
.bulk-action-group .button {
margin: 0;
}
.title-add-form {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.title-add-form input[type="text"] {
width: 180px;
}
.title-add-form select {
width: 100px;
}
</style>
<div class="bulk-action-section">
<!-- 原有功能按钮组 -->
<div class="bulk-action-group">
<a href="?publish_all_drafts=1" class="button button-primary"
onclick="return confirm('确定要发布所有草稿吗?此操作不可逆。\n将处理 <?php echo $draft_count; ?> 篇草稿。');">
📤 一键发布所有草稿
</a>
<a href="?republish_all=1" class="button button-primary"
onclick="return confirm('确定要更新所有已发布文章的发布时间吗?此操作不可逆。\n将处理 <?php echo $publish_count; ?> 篇文章。');">
⏰ 一键更新发布时间
</a>
<a href="?move_to_draft=1" class="button"
onclick="return confirm('确定要将所有已发布文章移动到草稿箱吗?此操作不可逆。\n将处理 <?php echo $publish_count; ?> 篇文章。');">
📥 一键移动到草稿箱
</a>
</div>
<!-- 新增:一键在标题前后添加名称功能 -->
<div class="bulk-action-group">
<form method="post" action="" style="display:flex; gap:8px; align-items:center;"
onsubmit="return confirm('确定要对所有已发布文章执行标题批量添加操作吗?此操作不可逆。\n将处理 <?php echo $publish_count; ?> 篇文章。');">
<?php wp_nonce_field('bulk_title_add_action', 'title_add_nonce'); ?>
<input type="text" name="title_add_text" placeholder="输入要添加的名称" required>
<select name="title_add_position">
<option value="prefix">添加到标题前</option>
<option value="suffix">添加到标题后</option>
</select>
<input type="hidden" name="action" value="bulk_add_to_title">
<button type="submit" class="button">✏️ 一键批量添加名称</button>
</form>
</div>
</div>
<?php
}
}
// 使用 admin_notices 钩子显示(与原有一致)
add_action( 'admin_notices', 'enhanced_admin_post_bulk_buttons' );
/**
* 获取指定状态文章数量(复用原有函数,增强兼容)
*/
function get_posts_count($status) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = %s AND post_type = 'post'",
$status
));
}
// ---------- 原有功能处理函数(稍作整合,保留原有逻辑)----------
// 处理发布所有草稿
function handle_publish_all_drafts() {
if ( isset( $_GET['publish_all_drafts'] ) && $_GET['publish_all_drafts'] == '1' ) {
if ( !current_user_can( 'publish_posts' ) ) {
wp_die('你没有权限执行此操作。');
}
global $wpdb;
$result = $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = 'publish' WHERE post_status = 'draft' AND post_type = 'post'" );
if ( $result !== false ) {
add_action( 'admin_notices', function() use ($result) {
echo '<div class="notice notice-success is-dismissible"><p>📤 已成功发布 ' . $result . ' 篇草稿文章。</p></div>';
});
} else {
add_action( 'admin_notices', function() {
echo '<div class="notice notice-error is-dismissible"><p>❌ 发布过程中出现错误,请检查数据库。</p></div>';
});
}
}
}
add_action( 'admin_init', 'handle_publish_all_drafts' );
// 处理一键更新发布时间
function handle_republish_all() {
if ( isset( $_GET['republish_all'] ) && $_GET['republish_all'] == '1' ) {
if ( !current_user_can( 'publish_posts' ) ) {
wp_die('你没有权限执行此操作。');
}
global $wpdb;
$current_time = current_time('mysql');
$current_time_gmt = get_gmt_from_date($current_time);
$result = $wpdb->query(
"UPDATE {$wpdb->posts}
SET post_date = '{$current_time}',
post_date_gmt = '{$current_time_gmt}',
post_modified = '{$current_time}',
post_modified_gmt = '{$current_time_gmt}'
WHERE post_status = 'publish'
AND post_type = 'post'"
);
if ( $result !== false ) {
add_action( 'admin_notices', function() use ($result) {
echo '<div class="notice notice-success is-dismissible"><p>✅ 已成功更新 ' . $result . ' 篇文章的发布时间。</p></div>';
});
} else {
add_action( 'admin_notices', function() {
echo '<div class="notice notice-error is-dismissible"><p>❌ 更新过程中出现错误,请检查数据库。</p></div>';
});
}
}
}
add_action( 'admin_init', 'handle_republish_all' );
// 处理一键移动到草稿箱
function handle_move_to_draft() {
if ( isset( $_GET['move_to_draft'] ) && $_GET['move_to_draft'] == '1' ) {
if ( !current_user_can( 'publish_posts' ) ) {
wp_die('你没有权限执行此操作。');
}
global $wpdb;
$result = $wpdb->query(
"UPDATE {$wpdb->posts}
SET post_status = 'draft'
WHERE post_status = 'publish'
AND post_type = 'post'"
);
if ( $result !== false ) {
add_action( 'admin_notices', function() use ($result) {
echo '<div class="notice notice-warning is-dismissible"><p>📥 已成功将 ' . $result . ' 篇文章移动到草稿箱。</p></div>';
});
} else {
add_action( 'admin_notices', function() {
echo '<div class="notice notice-error is-dismissible"><p>❌ 移动过程中出现错误,请检查数据库。</p></div>';
});
}
}
}
add_action( 'admin_init', 'handle_move_to_draft' );
// ---------- 新增:一键在标题前后添加名称的处理函数 ----------
/**
* 处理批量标题添加名称
*/
function handle_bulk_add_to_title() {
// 检查是否是我们的表单提交
if ( isset( $_POST['action'] ) && $_POST['action'] === 'bulk_add_to_title' ) {
// 验证 nonce
if ( ! isset( $_POST['title_add_nonce'] ) || ! wp_verify_nonce( $_POST['title_add_nonce'], 'bulk_title_add_action' ) ) {
wp_die('安全验证失败,请刷新页面重试。');
}
// 权限检查
if ( ! current_user_can( 'edit_others_posts' ) ) { // 需要编辑他人文章的权限
wp_die('你没有权限执行此操作。');
}
// 获取参数
$add_text = trim( sanitize_text_field( $_POST['title_add_text'] ) );
$position = sanitize_text_field( $_POST['title_add_position'] );
if ( empty( $add_text ) ) {
wp_die('添加的名称不能为空。');
}
if ( ! in_array( $position, array( 'prefix', 'suffix' ) ) ) {
wp_die('无效的操作位置。');
}
global $wpdb;
// 获取所有已发布文章的 ID 和标题
$posts = $wpdb->get_results(
"SELECT ID, post_title FROM {$wpdb->posts}
WHERE post_status = 'publish'
AND post_type = 'post'"
);
$updated_count = 0;
$errors = 0;
foreach ( $posts as $post ) {
$new_title = '';
if ( $position === 'prefix' ) {
$new_title = $add_text . ' ' . $post->post_title;
} else { // suffix
$new_title = $post->post_title . ' ' . $add_text;
}
// 使用 wp_update_post 更新标题(会触发钩子,更安全)
$update_result = wp_update_post( array(
'ID' => $post->ID,
'post_title' => $new_title
), true );
if ( is_wp_error( $update_result ) ) {
$errors++;
} else {
$updated_count++;
}
}
if ( $updated_count > 0 || $errors === 0 ) {
add_action( 'admin_notices', function() use ( $updated_count, $errors ) {
$message = '✏️ 已成功为 ' . $updated_count . ' 篇文章的标题' . ( $errors ? ',失败 ' . $errors . ' 篇。' : '。' );
echo '<div class="notice notice-success is-dismissible"><p>' . $message . '</p></div>';
});
} else {
add_action( 'admin_notices', function() {
echo '<div class="notice notice-error is-dismissible"><p>❌ 标题添加过程中出现错误,请检查。</p></div>';
});
}
}
}
add_action( 'admin_init', 'handle_bulk_add_to_title' );
?>