Title: post_preview
Published: April 25, 2014
Last modified: February 24, 2026

---

# post_preview(): string

## In this article

 * [Return](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#wp--skip-link--target)

Saves a draft or manually autosaves for the purpose of showing a post preview.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#return)󠁿

 string URL to redirect to show the preview.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#source)󠁿

    ```php
    function post_preview() {

    	$post_id     = (int) $_POST['post_ID'];
    	$_POST['ID'] = $post_id;

    	$post = get_post( $post_id );

    	if ( ! $post ) {
    		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
    	}

    	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
    		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
    	}

    	$is_autosave = false;

    	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() === (int) $post->post_author
    		&& ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status )
    	) {
    		$saved_post_id = edit_post();
    	} else {
    		$is_autosave = true;

    		if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) {
    			$_POST['post_status'] = 'draft';
    		}

    		$saved_post_id = wp_create_post_autosave( $post->ID );
    	}

    	if ( is_wp_error( $saved_post_id ) ) {
    		wp_die( $saved_post_id->get_error_message() );
    	}

    	$query_args = array();

    	if ( $is_autosave && $saved_post_id ) {
    		$query_args['preview_id']    = $post->ID;
    		$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );

    		if ( isset( $_POST['post_format'] ) ) {
    			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
    		}

    		if ( isset( $_POST['_thumbnail_id'] ) ) {
    			$query_args['_thumbnail_id'] = ( (int) $_POST['_thumbnail_id'] <= 0 ) ? '-1' : (int) $_POST['_thumbnail_id'];
    		}
    	}

    	return get_preview_post_link( $post, $query_args );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/post.php#L2083)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/post.php#L2083-L2134)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#related)󠁿

| Uses | Description | 
| [get_preview_post_link()](https://developer.wordpress.org/reference/functions/get_preview_post_link/)`wp-includes/link-template.php` |

Retrieves the URL used for the post preview.

  | 
| [wp_check_post_lock()](https://developer.wordpress.org/reference/functions/wp_check_post_lock/)`wp-admin/includes/post.php` |

Determines whether the post is currently being edited by another user.

  | 
| [wp_create_post_autosave()](https://developer.wordpress.org/reference/functions/wp_create_post_autosave/)`wp-admin/includes/post.php` |

Creates autosave data for the specified post from `$_POST` data.

  | 
| [edit_post()](https://developer.wordpress.org/reference/functions/edit_post/)`wp-admin/includes/post.php` |

Updates an existing post with values provided in `$_POST`.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [sanitize_key()](https://developer.wordpress.org/reference/functions/sanitize_key/)`wp-includes/formatting.php` |

Sanitizes a string key.

  | 
| [wp_create_nonce()](https://developer.wordpress.org/reference/functions/wp_create_nonce/)`wp-includes/pluggable.php` |

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

  | 
| [wp_die()](https://developer.wordpress.org/reference/functions/wp_die/)`wp-includes/functions.php` |

Kills WordPress execution and displays HTML page with an error message.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 8 more](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/post_preview/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_preview%2F)
before being able to contribute a note or feedback.