Dashboard Quick Draft widget for custom post type?
-
Hello,
I am building a multi-author site where users can log in and publish their own literary texts, for which I am using a custom post type called ‘text’. I’d like my users to be able to use the dashboard widget ‘Quick Draft’ to create text drafts which they can later access and edit via the ‘edit.php?post_type=text’ screen. Is it possible to modify the current Quick Draft widget to accept custom post types?
I have tried creating my own custom widget by copying and modifying the existing wp_dashboard_quick_press code, like so:
/** * Create a custom Quick Draft widget for the custom post type 'text'. * * @global int $post_ID * * @param string $error_msg Optional error message. Default (bool) false. * * @see wp_dashboard_quick_press() */ function dashboard_quick_draft( $error_msg = false ) { global $post_ID; $custom_post_type = 'text'; if ( ! current_user_can( 'edit_texts' ) ) { return; } /* Check if a new auto-draft (= no new post_ID) is needed or if the old can be used */ $last_post_id = (int) get_user_option( 'dashboard_quick_press_last_post_id' ); // Get the last post_ID. if ( $last_post_id ) { $post = get_post( $last_post_id ); if ( empty( $post ) || 'auto-draft' !== $post->post_status ) { // auto-draft doesn't exist anymore. $post = get_default_post_to_edit( "{$custom_post_type}", true ); update_user_option( get_current_user_id(), 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID. } else { $post->post_title = ''; // Remove the auto draft title. } } else { $post = get_default_post_to_edit( "{$custom_post_type}", true ); $user_id = get_current_user_id(); // Don't create an option if this is a super admin who does not belong to this site. if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) { update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID. } } $post_ID = (int) $post->ID; ?> <form name="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>" method="post" id="quick-press" class="initial-form hide-if-no-js"> <?php if ( $error_msg ) : ?> <div class="error"><?php echo $error_msg; ?></div> <?php endif; ?> <div class="input-text-wrap" id="title-wrap"> <label for="title"> <?php /** This filter is documented in wp-admin/edit-form-advanced.php */ echo apply_filters( 'enter_title_here', __( 'Title' ), $post ); ?> </label> <input type="text" name="post_title" id="title" autocomplete="off" /> </div> <div class="textarea-wrap" id="description-wrap"> <label for="content"><?php _e( 'Content' ); ?></label> <textarea name="content" id="content" placeholder="<?php esc_attr_e( 'Unleash your imagination...' ); ?>" class="mceEditor" rows="3" cols="15" autocomplete="off"></textarea> </div> <p class="submit"> <input type="hidden" name="action" id="quickpost-action" value="post-quickdraft-save" /> <input type="hidden" name="post_ID" value="<?php echo $post_ID; ?>" /> <input type="hidden" name="post_type" value="<?=$custom_post_type?>" /> <?php wp_nonce_field( 'add-post' ); ?> <?php submit_button( __( 'Save Draft' ), 'primary', 'save', false, array( 'id' => 'save-post' ) ); ?> <br class="clear" /> </p> </form> <?php wp_dashboard_recent_drafts(); }This doesn’t work, however: the custom widget just “hangs” when I press the submit button and I am presented with the following error in the browser console:
POST https://mysite.test/wp-admin/load-scripts.php?c=1&load%5Bchunk_0%5D=jquery-core,utils,moxiejs,plupload&ver=5.5.3 403 (Forbidden)I have some coding experience but I am far from an expert when it comes to interpreting these types of error messages. Any pointers that anyone can give me (either on fixing my custom widget or how to get the default widget to accept custom post types) will be much appreciated.
Cheers!
/jen
The topic ‘Dashboard Quick Draft widget for custom post type?’ is closed to new replies.