wppaste
WordPress

post_preview(): string

Since
2.7.0
Source
wp-admin/includes/post.php:2065
Saves a draft or manually autosaves for the purpose of showing a post preview.

Compatibility

WordPress
since 2.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

string
URL to redirect to show the preview.

Performance profile

How much work a call to post_preview() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
46–121

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 129.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()one call below post_preview()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 72 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost post_preview() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
current_user_can() && wp_check_post_lock() && !is_wp_error()46–54get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()49–53get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error()51–59get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()52–66get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error()52–60get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && !is_wp_error()52–60get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()54–58get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()55–59get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()55–59get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()57–71get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error()57–65get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error()57–65get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
60 further outcomes, up to 121 instructions
!current_user_can() && !wp_check_post_lock() && !is_wp_error()58–72get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()58–72get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error()58–66get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()60–64get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()60–64get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()61–65get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && !is_wp_error()63–87get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()63–77get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()63–77get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error()63–71get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()64–78get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()66–86get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()66–70get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error()68–92get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()69–99get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error()69–93get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && !is_wp_error()69–93get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()69–83get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()71–91get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()72–92get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()72–92get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()74–104get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error()74–98get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error()74–98get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)75–92get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()75–105get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error()75–105get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error()75–99get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()77–97get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()77–97get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)78–91get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()78–98get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)80–97get_post(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()80–110get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error()80–110get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error()80–104get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)81–104get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)81–98get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)81–98get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error()81–111get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)83–96get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()83–103get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)84–97get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)84–97get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)86–109get_post(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)86–103get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)86–103get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error()86–116get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)87–110get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)87–110get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)87–104get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)89–102get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)89–102get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)90–103get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)92–115get_post(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)92–115get_post(), __(), wp_die(), current_user_can(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)92–109get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && !is_wp_error() && isset($value) && !empty($value)93–116get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)95–108get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), edit_post(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()
!current_user_can() && !wp_check_post_lock() && is_wp_error() && isset($value) && !empty($value)98–121get_post(), __(), wp_die(), current_user_can(), __(), wp_die(), wp_check_post_lock(), get_current_user_id(), wp_create_post_autosave(), is_wp_error(), ->get_error_message(), wp_die(), wp_create_nonce(), sanitize_key(), get_preview_post_link()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12846–120151 fewer instruction than PHP 8.5
8.512946–12115
8.412946–12115
8.312946–12115
8.212946–12115
8.112946–121151 fewer instruction than PHP 7.4
7.413046–12215

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 12

Source code

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 );}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-admin/includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.