wppaste
WordPress

_wp_translate_postdata( bool $update = false, array|null $post_data = null ): array|WP_Error

Since
2.6.0
Source
wp-admin/includes/post.php:21
Renames $_POST data from form names to DB post columns.

Description

Manipulates $_POST directly.

Compatibility

WordPress
since 2.6.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.

Parameters

$updatebooloptional
Whether the post already exists.Default: false
$post_dataarray|nulloptional
The array of post data to process.
Defaults to the $_POST superglobal.Default: null

Return value

array|WP_Error
Array of post data on success, WP_Error on failure.

Performance profile

How much work a call to _wp_translate_postdata() 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
78–174

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below _wp_translate_postdata()
  • querycontent queryget_post()one call below _wp_translate_postdata()

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 · 20 distinct outcomes

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

WhenInstructionsCalls it makes
current_user_can()78get_post_type_object(), current_user_can(), get_current_user_id()
current_user_can()89–90get_post_type_object(), current_user_can(), get_current_user_id(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && !$jj138–142get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), __()
current_user_can() && !$aa && !$mm && !$jj141–148get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate()
current_user_can() && !$aa && !$mm && $jj142–145get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), __()
current_user_can() && !$aa && !$mm && $jj145–151get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate()
current_user_can() && !$aa && !$mm && !$jj146–153get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field()
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false150–154get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_gmt_from_date()
current_user_can() && !$aa && !$mm && $jj150–156get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field()
current_user_can() && !$aa && !$mm && !$jj152–160get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false154–157get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_gmt_from_date()
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false155–159get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date()
8 further outcomes, up to 174 instructions
current_user_can() && !$aa && !$mm && $jj156–163get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && !$jj157–165get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false159–162get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date()
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false161–166get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_gmt_from_date(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && $jj161–168get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false165–169get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_gmt_from_date(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && !$jj && $previous_date !== false166–171get_post_type_object(), current_user_can(), get_current_user_id(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date(), get_taxonomy(), current_user_can()
current_user_can() && !$aa && !$mm && $jj && $previous_date !== false170–174get_post_type_object(), current_user_can(), get_current_user_id(), gmdate(), sprintf(), wp_checkdate(), get_post_field(), get_gmt_from_date(), get_taxonomy(), current_user_can()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev44578–172705 fewer instructions than PHP 8.5
8.545078–17470
8.445078–17470
8.345078–17470
8.245078–17470
8.145078–174703 fewer instructions than PHP 7.4
7.445378–17770

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 · 11

Used by · 5

Source code

function _wp_translate_postdata( $update = false, $post_data = null ) { 	if ( empty( $post_data ) ) {		$post_data = &$_POST;	} 	if ( $update ) {		$post_data['ID'] = (int) $post_data['post_ID'];	} 	$ptype = get_post_type_object( $post_data['post_type'] ); 	if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {		if ( 'page' === $post_data['post_type'] ) {			return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );		} else {			return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );		}	} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {		if ( 'page' === $post_data['post_type'] ) {			return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );		} else {			return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );		}	} 	if ( isset( $post_data['content'] ) ) {		$post_data['post_content'] = $post_data['content'];	} 	if ( isset( $post_data['excerpt'] ) ) {		$post_data['post_excerpt'] = $post_data['excerpt'];	} 	if ( isset( $post_data['parent_id'] ) ) {		$post_data['post_parent'] = (int) $post_data['parent_id'];	} 	if ( isset( $post_data['trackback_url'] ) ) {		$post_data['to_ping'] = $post_data['trackback_url'];	} 	$post_data['user_ID'] = get_current_user_id(); 	if ( ! empty( $post_data['post_author_override'] ) ) {		$post_data['post_author'] = (int) $post_data['post_author_override'];	} else {		if ( ! empty( $post_data['post_author'] ) ) {			$post_data['post_author'] = (int) $post_data['post_author'];		} else {			$post_data['post_author'] = (int) $post_data['user_ID'];		}	} 	if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] !== $post_data['user_ID'] )		&& ! current_user_can( $ptype->cap->edit_others_posts ) ) { 		if ( $update ) {			if ( 'page' === $post_data['post_type'] ) {				return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );			} else {				return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );			}		} else {			if ( 'page' === $post_data['post_type'] ) {				return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );			} else {				return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );			}		}	} 	if ( ! empty( $post_data['post_status'] ) ) {		$post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 		// No longer an auto-draft.		if ( 'auto-draft' === $post_data['post_status'] ) {			$post_data['post_status'] = 'draft';		}

Changelog

Introduced in 2.6.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.9.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.