在 WordPress 后台管理大量文章时,逐个修改文章状态或发布时间会非常繁琐。本文将分享一个实用的子比主题定制功能,通过添加几行代码,即可在文章列表页实现一键发布所有草稿、批量更新发布时间、一键移所有已发布文章至草稿箱,大幅提升后台管理效率。
功能说明
本次添加的功能包含三个核心操作,均在文章列表页(
编辑文章页面)生效:- 一键发布所有草稿文章(不可逆);
- 一键更新所有已发布文章的发布 / 修改时间为当前时间;
- 一键将所有已发布文章移至草稿箱(不可逆)。
所有操作均包含权限验证和操作确认提示,避免误操作,同时执行后会给出明确的成功 / 失败反馈。
实现步骤
第一步:备份文件
在修改代码前,务必备份子比主题的
functions.php 文件(路径:/wp-content/themes/zibll/functions.php),防止操作失误导致网站异常。第二步:添加代码
将以下完整代码复制,粘贴到子比主题
functions.php 文件的末尾,保存即可。// 在管理员后台添加一个一键发布所有草稿的按钮
function admin_post_publish_all_drafts_button() {
global $current_screen;
// 仅在文章列表页面显示
if ( 'edit-post' == $current_screen->id ) {
echo '<a href="?publish_all_drafts=1" class="page-title-action" onclick="return confirm(\'确定要发布所有草稿吗?此操作不可逆。\');">一键发布所有草稿</a>';
}
}
add_action( 'admin_notices', 'admin_post_publish_all_drafts_button' );
// 处理发布请求
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 admin_post_bulk_actions_buttons() {
global $current_screen;
// 仅在文章列表页面显示
if ( 'edit-post' == $current_screen->id ) {
?>
<div style="margin: 8px 0; display: flex; gap: 10px;">
<a href="?republish_all=1" class="button button-primary"
onclick="return confirm('确定要更新所有已发布文章的发布时间吗?此操作不可逆。\n将处理 <?php echo get_posts_count('publish'); ?> 篇文章。');">
一键更新发布时间
</a>
<a href="?move_to_draft=1" class="button"
onclick="return confirm('确定要将所有已发布文章移动到草稿箱吗?此操作不可逆。\n将处理 <?php echo get_posts_count('publish'); ?> 篇文章。');">
一键移动到草稿箱
</a>
</div>
<?php
}
}
add_action( 'admin_notices', 'admin_post_bulk_actions_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_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' );
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


一键移动到草稿箱
</a>
</div>
<?php
}
}
add_action( 'admin_notices', 'admin_post_bulk_actions_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_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' );


























暂无评论内容