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

1 Answer
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;
}

Leave a Comment