WordPress.org

Ready to get started?Download WordPress

Forums

Howto: Add category selection to QuickPress (17 posts)

  1. Mark / t31os
    Moderator
    Posted 4 years ago #

    Hi all, figured i'd share something i've managed to do and i've not seen anyone else yet document, and it is somewhat of a hack, but it does work.

    I thought about writing a blog entry about this, but felt since i spend more time here, and this site is far more active, that it would make more sense to post it here directly...

    I'm sure alot of users want to use QuickPress but still have the ability to select a category for the entries being made. Of course this only covers adding one category, but i decided the option to select one over no selection at all might be of interest to someone.

    Here's how it's done.

    Add the following to your theme's functions.php file:

    function __quickpress_cats() {
    	if(!function_exists('is_admin') || !is_admin()) return;
    	// Parameters for wp_dropdown_categories
    	$args = array(
    		'order' => 'ASC',
    		'hierarchical' => 1,
    		'echo' => 0,
    		'name' => 'post_category[]',
    		'hide_empty' => 0
    	);
    	$select_cats = wp_dropdown_categories( $args );
    	echo '
    	</div>
    	<h4 id="category-label"><label for="post_category">Category</label></h4>
    	<div>
    	'.$select_cats.'
    	</div>
    	<div class="hide-if-no-js" style="margin: 0 0 .5em 5em;padding:8px 10px 5px 5px;">
    	';
    	return;
    }
    if(is_admin() || $query->is_admin) {
    	$here = array( basename($_SERVER['REQUEST_URI']), basename($_SERVER['SCRIPT_FILENAME']) );
    
    	if( ( $here[0] == ('index.php' || 'wp-admin')) && ( $here[1] == 'index.php') ) {
    		add_action('media_buttons','__quickpress_cats');
    	}
    	unset($here);
    }

    The code below the function makes a basic check to see what page you're on, so as to not hook onto the media-buttons in other places..

    Yes it's a little hacky, and may not be rocket science, but it does function and give you the ability to choose a category when making QuickPress posts...

    Enjoy!.. :)

  2. johnywhy
    Member
    Posted 4 years ago #

    great hack, but there's a bug.

    after you submit, you get a fresh, empty quickpress screen. except this time, there's no category picker.

    thanks!

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Hi johnywhy,

    I've actually found a better way to do this, it's much more reliable and works for dual category selection to (if you don't mind a few lines of jQuery in addition). There's no way to unregister widgets from the dashboard (at present), however there is a way to unset or set data in the widget/metabox array that deals with what's shown on the dashboard.

    Importantly this means i can change the callback function for quickpress, or in other words i can update the QuickPress widget to call my own function in place of the default function..

    I'll post something up over the next few days.. It'll improve on the above code somewhat.. :)

  4. physman
    Member
    Posted 4 years ago #

    Hi t31os,

    Have you figured out how to get this to work better? I'm setting up a PMU installation for our school and find that this would be the easiest way for my teachers to be able to post about two different grades on one installation. I also have that same problem as johnywhy. Any help you could give would be appreciated.

    Thanks.

  5. Pete
    Member
    Posted 4 years ago #

    i think i read somewhere that 3.0 might include this?

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Yes, sorry guys, was writing a plugin that included this, but ditched the plugin (got bored with it and started something else), i'll wrap something up for you to use though as i did say i would..

    I don't believe 3.0 includes this, or at least it doesn't yet. I'm running 3.0 nightly on the local install, and there's no extra category selection on the quickpress widget thus far....

    Give me a few hours and i'll post something up for you.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Create a file, call it what you want, for example "quickpress-category.php"...

    Place it into your plugins folder (inside it's own folder if you like, it'll work either way), and then add the following code.

    http://wordpress.pastebin.ca/1736479

    Save the file, load the plugins list up, and activate the plugin.

    After you do that, go to Settings > Writing , and check the box for "Enable quickpress category selection." and click Save.

    Now once you load the dashboard you'll see there's an additional category dropdown box.

    Does nothing more, and nothing less... :)

  8. physman
    Member
    Posted 4 years ago #

    It works perfectly like it should when you first log in and write your first post. After pushing publish the category box disappears. Same problem as adding that code in the function file. I believe after you push the publish button it goes to the part of the dashboard.php

    function wp_dashboard_quick_press() {
    	$drafts = false;
    	if ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['action'] ) && 0 === strpos( $_POST['action'], 'post-quickpress' ) && (int) $_POST['post_ID'] ) {
    		$view = get_permalink( $_POST['post_ID'] );
    		$edit = esc_url( get_edit_post_link( $_POST['post_ID'] ) );
    		if ( 'post-quickpress-publish' == $_POST['action'] ) {
    			if ( current_user_can('publish_posts') )
    				printf( '<div class="message"><p>' . __( 'Post Published. <a href="%s">View post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( $view ), $edit );
    			else
    				printf( '<div class="message"><p>' . __( 'Post submitted. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
    		} else {
    			printf( '<div class="message"><p>' . __( 'Draft Saved. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
    			$drafts_query = new WP_Query( array(
    				'post_type' => 'post',
    				'post_status' => 'draft',
    				'author' => $GLOBALS['current_user']->ID,
    				'posts_per_page' => 1,
    				'orderby' => 'modified'

    I'm not sure exactly where it will pull up a different part of the file to make that disappear.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    The reason that happens is because post.php calls up the old function after publish (via include, for the ajax), thus loading the original non-category enabled function, at least until the dashboard page is loaded again.

    There aren't any hooks for me to stop this happening, i can only suggest reloading the dashboard page when this happens.

  10. @mercime
    Volunteer Moderator
    Posted 4 years ago #

    Cool t31os_! Will be testing your code this weekend. I had disabled QuickPres in WP and WPMU dashboards because users kept forgetting to add the categories in Write panel, grrrr :-)

  11. physman
    Member
    Posted 4 years ago #

    Thank you very much for all of your effort. I'm going to set it up this way for now. I'll have my users use it like this for a while, then I'll look through the code and see if there is anything that I can find that might be able to work without making it disappear. Maybe I can find something in there. Who knows, it might not be possible at all. I think this would be a very nice feature for them to implement in future releases of wordpress.

    You've really saved me users a lot of headache. If you're ever in Taipei let me buy you a cup of coffee!

  12. Mark / t31os
    Moderator
    Posted 4 years ago #

    Happy to help guys.

    Physman not sure i'm ever likely to be in Taipei, but should i be i'd welcome a good coffee, thanx.. ;)

    Let me know if you find any problems (aside from the one discussed), hopefully you shouldn't it's basically a copy of the original widget, with some minor additions.

  13. flyboy-sd
    Member
    Posted 4 years ago #

    Good call. I'd love this functionality but couldn't get this link to work:
    http://wordpress.pastebin.ca/1736479

    Is it still around or available in some other form?

    Thanks!

  14. flyboy-sd
    Member
    Posted 4 years ago #

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    I'm glad you replied, it's prompted a reminder for me to a submit a ticket at some point to add a small change to the code involved with this widget.

    Glad you were able to find it, although you can always just plonk the address in google and use the "cached" link to see the cached version.. ;) Just an FYI.. :)

  16. Stephanie Leary
    Member
    Posted 4 years ago #

    The newer version doesn't appear to work in 3.0 beta. The older code from the first post does, though.

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    I shall be submitting a trac ticket to ask that some hooks are placed into the QuickPress widget as soon as i get time to run some additional tests.

    Should be a very simple patch, and i would like to be providing a patch with the ticket so it has a better chance of being added into core sooner (if it's accepted). A few minor problems prevent me from doing that right now, working out how to handle tab-indexes, and deciding whether to add in custom taxonomy support(it would work i believe, been looking at the code involved over the last few days).

    This would mean, rather than replacing the whole callback function with another (ie. what's been done in the above code), instead hook onto a new action and insert the extra form elements you need. I've already tested some hooks in the widget, and it's definately an easy and workable addition, it's a just matter of putting together a suitable patch.

    EDIT: Actually why wait, ticket submitted.

    NOTE: Sillybean i believe i know what the problem is with whichever version isn't working under 3.0, but you'll need to point out which specific version you're referring to, so i can correct it, the codex one, or the pastebin one?

    If you'd like to see better control over the QuickPress widget, do your part and vote for the ticket on the ideas section, here.

Topic Closed

This topic has been closed to new replies.

About this Topic