Site icon Hip-Hop Website Design and Development

Publish post when edit post form submitted with enter/return pressed on keyboard

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 );