函数文档

redirect_post()

💡 云策文档标注

概述

redirect_post() 是 WordPress 中的一个函数,用于在文章保存或发布后重定向到前一页面。它根据不同的操作(如保存、发布、添加或删除元数据)设置重定向 URL,并应用过滤器允许开发者自定义重定向目标。

关键要点

  • 函数 redirect_post() 处理文章保存或发布后的重定向逻辑。
  • 参数 $post_id 可选,默认为 0,用于指定文章 ID。
  • 根据 $_POST 参数(如 'save'、'publish'、'addmeta'、'deletemeta')决定重定向目标和消息参数。
  • 使用 add_query_arg() 添加消息参数到重定向 URL,消息值基于文章状态或操作类型。
  • 应用过滤器 'redirect_post_location' 允许开发者修改重定向 URL。
  • 最终调用 wp_redirect() 执行重定向并退出。

代码示例

function redirect_post( $post_id = 0 ) {
    if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {
        $status = get_post_status( $post_id );
        switch ( $status ) {
            case 'pending':
                $message = 8;
                break;
            case 'future':
                $message = 9;
                break;
            case 'draft':
                $message = 10;
                break;
            default:
                $message = isset( $_POST['publish'] ) ? 6 : 1;
                break;
        }
        $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
    } elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) {
        $location = add_query_arg( 'message', 2, wp_get_referer() );
        $location = explode( '#', $location );
        $location = $location[0] . '#postcustom';
    } elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) {
        $location = add_query_arg( 'message', 3, wp_get_referer() );
        $location = explode( '#', $location );
        $location = $location[0] . '#postcustom';
    } else {
        $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
    }
    wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
    exit;
}

注意事项

  • 函数依赖于 $_POST 参数来触发重定向,确保在适当的表单提交上下文中使用。
  • 重定向 URL 可能包含消息参数(如 'message'),用于在前端显示状态信息。
  • 过滤器 'redirect_post_location' 允许自定义重定向逻辑,但需谨慎处理以避免无限重定向。
  • 函数在重定向后调用 exit,确保脚本终止执行。

📄 原文内容

Redirects to previous page.

Parameters

$post_idintoptional
Post ID.

Source

function redirect_post( $post_id = 0 ) {
	if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {
		$status = get_post_status( $post_id );

		switch ( $status ) {
			case 'pending':
				$message = 8;
				break;
			case 'future':
				$message = 9;
				break;
			case 'draft':
				$message = 10;
				break;
			default:
				$message = isset( $_POST['publish'] ) ? 6 : 1;
				break;
		}

		$location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
	} elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) {
		$location = add_query_arg( 'message', 2, wp_get_referer() );
		$location = explode( '#', $location );
		$location = $location[0] . '#postcustom';
	} elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) {
		$location = add_query_arg( 'message', 3, wp_get_referer() );
		$location = explode( '#', $location );
		$location = $location[0] . '#postcustom';
	} else {
		$location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
	}

	/**
	 * Filters the post redirect destination URL.
	 *
	 * @since 2.9.0
	 *
	 * @param string $location The destination URL.
	 * @param int    $post_id  The post ID.
	 */
	wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
	exit;
}

Hooks

apply_filters( ‘redirect_post_location’, string $location, int $post_id )

Filters the post redirect destination URL.

Changelog

Version Description
2.7.0 Introduced.