Quick Draft for Pages and Custom Post Types
Adds a dashboard widget like Quick Draft that lets you start a draft of any post type, such as a page, product or event, with a title and a few notes.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_quick_draft_any', 'Quick draft (any type)', function () {
$types = array_filter( get_post_types( array( 'show_ui' => true, 'public' => true ), 'objects' ), function ( $type ) {
return 'attachment' !== $type->name && current_user_can( $type->cap->edit_posts );
} );
if ( ! empty( $_GET['wpide_qd'] ) ) {
$id = absint( $_GET['wpide_qd'] );
printf( '<div class="notice notice-success inline"><p>Draft saved. <a href="%s">Continue editing</a></p></div>', esc_url( get_edit_post_link( $id ) ) );
}
?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<input type="hidden" name="action" value="wpide_quick_draft">
<?php wp_nonce_field( 'wpide_quick_draft', 'wpide_qd_nonce' ); ?>
<p><label for="wpide-qd-type">Type</label><br>
<select id="wpide-qd-type" name="wpide_qd_type">
<?php foreach ( $types as $type ) : ?>
<option value="<?php echo esc_attr( $type->name ); ?>"><?php echo esc_html( $type->labels->singular_name ); ?></option>
<?php endforeach; ?>
</select></p>
<p><label for="wpide-qd-title">Title</label><br><input id="wpide-qd-title" name="wpide_qd_title" type="text" class="widefat" required></p>
<p><label for="wpide-qd-content">Notes</label><br><textarea id="wpide-qd-content" name="wpide_qd_content" rows="3" class="widefat"></textarea></p>
<p><button class="button button-primary">Save draft</button></p>
</form>
<?php
} );
} );
add_action( 'admin_post_wpide_quick_draft', function () {
check_admin_referer( 'wpide_quick_draft', 'wpide_qd_nonce' );
$type = isset( $_POST['wpide_qd_type'] ) ? sanitize_key( $_POST['wpide_qd_type'] ) : 'post';
$object = get_post_type_object( $type );
if ( ! $object || ! current_user_can( $object->cap->edit_posts ) ) {
wp_die( 'Not allowed.', 403 );
}
$id = wp_insert_post( array(
'post_type' => $type,
'post_status' => 'draft',
'post_title' => isset( $_POST['wpide_qd_title'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_qd_title'] ) ) : '',
'post_content' => isset( $_POST['wpide_qd_content'] ) ? wp_kses_post( wp_unslash( $_POST['wpide_qd_content'] ) ) : '',
'post_author' => get_current_user_id(),
) );
wp_safe_redirect( add_query_arg( 'wpide_qd', (int) $id, admin_url( 'index.php' ) ) );
exit;
} );
Notes
After saving you get a link to continue editing the new draft.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.