• Resolved ninsan

    (@jennyrigsjo)


    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You have the right idea. Getting a 403 error often means the POST request has run afoul with the server’s modSecurity protections. It might be something your host would need to resolve for you.

    I’d be willing to try out your widget on my site to rule out modSecurity influence, but I’m short of time right now. To save me time, I have sort of a dumb question. I know I can find out for myself, but you obviously know the answer. How does your widget actually get to be displayed on the dashboard?

    Thread Starter ninsan

    (@jennyrigsjo)

    Hi @bcworkz , thank you for helping. Here is the code used to add the ‘dashboard_quick_draft’ widget to the dashboard:

    add_action('admin_init', 'jr_setup_dashboard');
    
    function jr_setup_dashboard() {
    
        global $pagenow;
    
        if (!is_admin() || $pagenow !== 'index.php') {
            return;
        }
    
        if (!current_user_can('administrator') && !current_user_can('editor')) {
            remove_meta_box( 'dashboard_quick_press', 'dashboard', 'normal' );
            add_action('wp_dashboard_setup', 'add_custom_quick_draft_widget');
        }
    }
    
    /**
     * Add the custom Quick Draft widget to the admin dashboard.
     */
    function add_custom_quick_draft_widget() {
        wp_add_dashboard_widget('jr_dashboard_quick_press', __( 'Quick Draft' ), 'dashboard_quick_draft');
    }

    The ‘jr_setup_dashboard’ function actually contains more code than what is shown here, but I have chosen to remove it for the sake of simplicity and relevance.

    /jen

    Moderator bcworkz

    (@bcworkz)

    Thanks, that saved me some research. In using your widget (configured to use one of my existing CPTs), I got a 400 Bad Request Response. In comparing yours and the default’s POST data, in the default there’s a {save: 'Save Draft'} element that’s not part of your widget’s POST data. It appears to be coming from some sort of submittal script, as the initiator of the POST request is load_scripts.php and not index.php as we might expect. It clearly is not part of the default form data, but somehow inserted later.

    The script apparently resets the form upon a successful save. With both widgets active the script doesn’t behave correctly. There may have been a name collision, which wouldn’t matter since you remove the default widget. But this script isn’t properly handling your widget either.

    That’s as far as I got in investigation. Note that I use “appears”, “apparently” and similar weasel words here. I’m not sure my findings here are fully accurate.

    Thread Starter ninsan

    (@jennyrigsjo)

    Hello again, just thought I’d let the forum mods know I have decided to put this issue to rest for now, as I have more pressing matters to attend to. Thank you so much for trying to help, @bcworkz, I really appreciate it. 🙂 Unfortunately I didn’t get much further than you in trying to figure out how to make my custom widget work. I’ve put the matter aside for now, but I might continue working on it in the future. If I ever find a solution or need more help, I will make another post here in the forum.

    Cheers,
    Jen

    Thread Starter ninsan

    (@jennyrigsjo)

    Just popping by to mark this as “resolved” in keeping with forum guidelines…

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Dashboard Quick Draft widget for custom post type?’ is closed to new replies.