WP_Customize_Nav_Menus::insert_auto_draft_post() WordPress Method

This method inserts an auto-draft post into the database for the new menu item. This is used when the user has not yet specified a title for the menu item.

WP_Customize_Nav_Menus::insert_auto_draft_post( array $postarr ) #

Adds a new auto-draft post.


Parameters

$postarr

(array)(Required)Post array. Note that post_status is overridden to be auto-draft


Top ↑

Return

(WP_Post|WP_Error) Inserted auto-draft post object or error.


Top ↑

Source

File: wp-includes/class-wp-customize-nav-menus.php

946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
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 );
    }
}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.