untrash_post
云策文档标注
概述
untrash_post 是一个 WordPress 动作钩子,在文章从回收站恢复之前触发。它允许开发者在恢复操作时执行自定义代码,例如处理多个文章恢复的场景。
关键要点
- untrash_post 钩子在文章从回收站恢复前触发,用于执行自定义操作。
- 钩子接受两个参数:$post_id(文章ID)和 $previous_status(文章被移至回收站前的状态)。
- 从 WordPress 5.6.0 版本开始添加了 $previous_status 参数,增强了钩子的功能。
- 开发者可以利用此钩子处理单个或多个文章恢复的逻辑,例如通过 $_GET['post'] 获取批量恢复的文章ID。
代码示例
function wpdocs_untrash_multiple_posts( $post_id = '' ) {
// 检查是否为批量恢复文章
if ( isset( $_GET['post'] ) && is_array( $_GET['post'] ) ) {
foreach( $_GET['post'] as $post_id ) {
wpdocs_your_function( $post_id );
}
} else {
wpdocs_your_function( $post_id );
}
}
add_action( 'untrash_post', 'wpdocs_untrash_multiple_posts' );注意事项
- untrash_post 钩子在每次文章恢复时都会触发,包括批量恢复操作,但开发者需注意代码逻辑以确保正确处理批量场景。
- 用户贡献笔记中提到,示例代码在单个文章恢复时工作正常,但在批量恢复时可能存在问题,建议开发者测试和调整代码以适应实际需求。
原文内容
Fires before a post is restored from the Trash.
Parameters
$post_idint-
Post ID.
$previous_statusstring-
The status of the post at the point where it was trashed.
Source
do_action( 'untrash_post', $post_id, $previous_status );
Skip to note 2 content
Riverlab
To get the id of all posts that have been restored when selecting multiples, you must use the
$_GET['post']Like that example below:
function wpdocs_untrash_multiple_posts( $post_id = '' ) { // Verify if is restoring multiple posts if ( isset( $_GET['post'] ) && is_array( $_GET['post'] ) ) { foreach( $_GET['post'] as $post_id ) { wpdocs_your_function( $post_id ); } } else { wpdocs_your_function( $post_id ); } } add_action( 'untrash_post', 'wpdocs_untrash_multiple_posts' );