admin管理员组

文章数量:1279148

How do you publish posts when the post edit form is submitted with the enter/return key on the keyboard? By default WordPress saves the post in draft status.

To provide some background, I have an Inventory custom post type, which does not use the Title or Content fields, only meta fields which are text inputs. To streamline input of Inventory posts, I want to publish the post when the author hits the enter/return key.

I've compared the $_POST variable of edit forms submitted both ways and used the differences to come up with the code below. Problem with this method though is many action hooks around save/publish are triggered twice, which is annoying if you have functions hooked to those actions.

/**
 * Publish Inventory items when Enter key pressed
 * @param  integer $post_id Post ID
 * @return void
 */
function kt_publish_inventory_item_on_enter( $post_id ) {
    Global $typenow;

    if ( 'inventory' == $typenow ) {
        $data = array('ID' => $post_id, 'post_status' => 'publish' );

        if ( 'auto-draft' == $_POST['original_post_status'] && 'draft' == $_POST['post_status'] ) {
            $update_status = wp_update_post( $data );
        }

    }

}
add_action( 'acf/save_post', 'kt_publish_inventory_item_on_enter', 10, 1 );

How do you publish posts when the post edit form is submitted with the enter/return key on the keyboard? By default WordPress saves the post in draft status.

To provide some background, I have an Inventory custom post type, which does not use the Title or Content fields, only meta fields which are text inputs. To streamline input of Inventory posts, I want to publish the post when the author hits the enter/return key.

I've compared the $_POST variable of edit forms submitted both ways and used the differences to come up with the code below. Problem with this method though is many action hooks around save/publish are triggered twice, which is annoying if you have functions hooked to those actions.

/**
 * Publish Inventory items when Enter key pressed
 * @param  integer $post_id Post ID
 * @return void
 */
function kt_publish_inventory_item_on_enter( $post_id ) {
    Global $typenow;

    if ( 'inventory' == $typenow ) {
        $data = array('ID' => $post_id, 'post_status' => 'publish' );

        if ( 'auto-draft' == $_POST['original_post_status'] && 'draft' == $_POST['post_status'] ) {
            $update_status = wp_update_post( $data );
        }

    }

}
add_action( 'acf/save_post', 'kt_publish_inventory_item_on_enter', 10, 1 );
Share Improve this question edited Jan 22, 2016 at 22:54 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Aug 2, 2015 at 13:16 AndrewAndrew 9199 silver badges20 bronze badges 1
  • Was my answer helpful to you?! Please accept and upvote if it was helpful – WordPress Speed Commented Mar 28, 2020 at 16:12
Add a comment  | 

1 Answer 1

Reset to default 1

you may add a filter on wp_insert_post_data:

add_filter( 'wp_insert_post_data', 'my_force_publish_post', 10, 2 );

function my_force_publish_post( $data, $postarr ) {
    if ( ! isset($postarr['ID']) || ! $postarr['ID'] ) return $data;
    if ( $postarr['post_type'] !== 'inventory' ) return $data;

    $old = get_post( $postarr['ID'] ); // the post before update
    if( $old->post_status !== 'draft' ) {
        // force a post to be set as published
        $data['post_status'] = 'publish'
    }
    return $data;
}

本文标签: Publish post when edit post form submitted with enterreturn pressed on keyboard