WP_Customize_Nav_Menus::insert_auto_draft_post( array $postarr ): WP_Post|WP_Error
- Since
- 4.7.0
- Source
wp-includes/class-wp-customize-nav-menus.php:955
auto-draft post.Compatibility
- WordPress
- since 4.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.
Parameters
$postarrarray- Post array. Note that post_status is overridden to be
auto-draft.$post_titlestringPost title. Required.$post_typestringPost type. Required.$post_namestringPost name.$post_contentstringPost content.
Return value
WP_Post|WP_Error- Inserted auto-draft post object or error.
Performance profile
How much work a call to WP_Customize_Nav_Menus::insert_auto_draft_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
- 11–59
- 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 84.
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()called directly - hookthird-party callbacks
apply_filters()one call below WP_Customize_Nav_Menus::insert_auto_draft_post() - optionoption read or write
get_option()one call below WP_Customize_Nav_Menus::insert_auto_draft_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_Customize_Nav_Menus::insert_auto_draft_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–15 | __() |
isset($postarr) && is_wp_error() | 47–49 | ->changeset_uuid(), add_filter(), wp_slash(), wp_insert_post(), remove_filter(), is_wp_error() |
isset($postarr) && !is_wp_error() | 50–52 | ->changeset_uuid(), add_filter(), wp_slash(), wp_insert_post(), remove_filter(), is_wp_error(), get_post() |
isset($postarr) && is_wp_error() | 54–56 | sanitize_title(), ->changeset_uuid(), add_filter(), wp_slash(), wp_insert_post(), remove_filter(), is_wp_error() |
isset($postarr) && !is_wp_error() | 57–59 | sanitize_title(), ->changeset_uuid(), add_filter(), wp_slash(), wp_insert_post(), remove_filter(), is_wp_error(), get_post() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 84 instructions, 11–59 executed per call, 6 branches. The work does not change between versions.
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 · 9
- __()Retrieves the translation of $text.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- add_filter()Adds a callback function to a filter hook.
- wp_insert_post()Inserts or update a post.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- remove_filter()Removes a callback function from a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_post()Retrieves post data given a post ID or post object.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()Ajax handler for adding a new auto-draft post.
Source code
public function insert_auto_draft_post( $postarr ) { if ( ! isset( $postarr['post_type'] ) ) { return new WP_Error( 'unknown_post_type', __( 'Invalid post type.' ) ); } if ( empty( $postarr['post_title'] ) ) { return new WP_Error( 'empty_title', __( 'Empty title.' ) ); } if ( ! empty( $postarr['post_status'] ) ) { return new WP_Error( 'status_forbidden', __( 'Status is forbidden.' ) ); } /* * If the changeset is a draft, this will change to draft the next time the changeset * is updated; otherwise, auto-draft will persist in autosave revisions, until save. */ $postarr['post_status'] = 'auto-draft'; // Auto-drafts are allowed to have empty post_names, so it has to be explicitly set. if ( empty( $postarr['post_name'] ) ) { $postarr['post_name'] = sanitize_title( $postarr['post_title'] ); } if ( ! isset( $postarr['meta_input'] ) ) { $postarr['meta_input'] = array(); } $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name']; $postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid(); unset( $postarr['post_name'] ); add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); $r = wp_insert_post( wp_slash( $postarr ), true ); remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); if ( is_wp_error( $r ) ) { return $r; } else { return get_post( $r ); } }Changelog
Introduced in 4.7.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.7.7 tag, from
src/wp-includes/class-wp-customize-nav-menus.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.