redirect_post()WP 2.7.0

Redirects to previous page.

Hooks from the function

Return

null. Nothing (null).

Usage

redirect_post( $post_id );
$post_id(int)
Post ID.
Default: ''

Changelog

Since 2.7.0 Introduced.

redirect_post() code WP 6.6.2

function redirect_post( $post_id = '' ) {
	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;
}