wp_write_post(): int|WP_Error
- Since
- 2.1.0
- Source
wp-admin/includes/post.php:908
$_POST information.Compatibility
- WordPress
- since 2.1.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
int|WP_Error- Post ID on success, WP_Error on failure.
Performance profile
How much work a call to wp_write_post() 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
- Scaling
- Constant
- Instructions
- 25–87
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 124.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below wp_write_post() - hookthird-party callbacks
apply_filters()one call below wp_write_post() - optionoption read or write
get_option()one call below wp_write_post()
Further down the call graph this can also reach 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 · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_write_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
current_user_can() && isset($value) | 25–29 | get_post_type_object(), current_user_can(), edit_post() |
!current_user_can() | 25–29 | get_post_type_object(), current_user_can(), __() |
!isset($value) && current_user_can() && is_wp_error() | 34–56 | get_post_type_object(), current_user_can(), _wp_translate_postdata(), is_wp_error() |
!isset($value) && current_user_can() && !is_wp_error() | 46–70 | get_post_type_object(), current_user_can(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_insert_post(), is_wp_error() |
!isset($value) && current_user_can() && !is_wp_error() && !empty($post_id) | 65–87 | get_post_type_object(), current_user_can(), _wp_translate_postdata(), is_wp_error(), _wp_get_allowed_postdata(), wp_insert_post(), is_wp_error(), add_meta(), add_post_meta(), _fix_attachment_links(), wp_set_post_lock() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 124 | 25–87 | 12 | |
| 8.5 | 124 | 25–87 | 12 | |
| 8.4 | 124 | 25–87 | 12 | |
| 8.3 | 124 | 25–87 | 12 | |
| 8.2 | 124 | 25–87 | 12 | 1 more instruction than PHP 8.1 |
| 8.1 | 123 | 25–87 | 12 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 124 | 25–88 | 12 |
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 · 13
- get_post_type_object()Retrieves a post type object by name.
- current_user_can()Returns whether the current user has the specified capability.
- __()Retrieves the translation of $text.
- edit_post()Updates an existing post with values provided in `$_POST`.
- _wp_translate_postdata()Renames `$_POST` data from form names to DB post columns.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _wp_get_allowed_postdata()Returns only allowed post data fields.
- wp_insert_post()Inserts or update a post.
- add_meta()Adds post meta data defined in the `$_POST` superglobal for a post with given ID.
- add_post_meta()Adds a meta field to the given post.
- _fix_attachment_links()Replaces hrefs of attachment anchors with up-to-date permalinks.
- wp_set_post_lock()Marks the post as currently being edited by the current user.
Show all 13
- WP_Error::__construct()Initializes the error.
Used by · 1
- write_post()Calls wp_write_post() and handles the errors.
Source code
function wp_write_post() { if ( isset( $_POST['post_type'] ) ) { $ptype = get_post_type_object( $_POST['post_type'] ); } else { $ptype = get_post_type_object( 'post' ); } if ( ! current_user_can( $ptype->cap->edit_posts ) ) { if ( 'page' === $ptype->name ) { return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) ); } else { return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) ); } } $_POST['post_mime_type'] = ''; // Clear out any data in internal vars. unset( $_POST['filter'] ); // Edit, don't write, if we have a post ID. if ( isset( $_POST['post_ID'] ) ) { return edit_post(); } if ( isset( $_POST['visibility'] ) ) { switch ( $_POST['visibility'] ) { case 'public': $_POST['post_password'] = ''; break; case 'password': unset( $_POST['sticky'] ); break; case 'private': $_POST['post_status'] = 'private'; $_POST['post_password'] = ''; unset( $_POST['sticky'] ); break; } } $translated = _wp_translate_postdata( false ); if ( is_wp_error( $translated ) ) { return $translated; } $translated = _wp_get_allowed_postdata( $translated ); // Create the post. $post_id = wp_insert_post( $translated ); if ( is_wp_error( $post_id ) ) { return $post_id; } if ( empty( $post_id ) ) { return 0; } add_meta( $post_id ); add_post_meta( $post_id, '_edit_last', $GLOBALS['current_user']->ID ); // Now that we have an ID we can fix any attachment anchor hrefs. _fix_attachment_links( $post_id ); wp_set_post_lock( $post_id ); return $post_id;}Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.