Wordpress - How to add category to: 'wp-admin/post-new.php'?

Hook into wp_insert_post, test the post status for auto-draft, and the URL for a GET parameter.

But first we need a helper function to get and sanitize the GET parameter:

/**
 * Set default category.
 *
 * @wp-hook pre_option_default_category
 * @return  string Category slug
 */
function t5_get_default_cat_by_url()
{
    if ( ! isset( $_GET['post_cat'] ) )
        return FALSE;

    return array_map( 'sanitize_title', explode( ',', $_GET['post_cat'] ) );
}

Now the auto-draft handler:

add_action( 'wp_insert_post', 't5_draft_category', 10, 2 );

/**
 * Add category by URL parameter to auto-drafts.
 *
 * @wp-hook wp_insert_post
 * @param   int $post_ID
 * @param   object $post
 * @return  WP_Error|array An error object or term ID array.
 */
function t5_draft_category( $post_ID, $post )
{
    if ( ! $cat = t5_get_default_cat_by_url()
        or 'auto-draft' !== $post->post_status )
        return;

    // return value will be used in unit tests only.
    return wp_set_object_terms( $post_ID, $cat, 'category' );
}

This works only if get_default_post_to_edit() was called with the second parameter $create_in_db set to TRUE. To catch the other case you have to filter the option default_category:

add_filter( 'pre_option_default_category', 't5_get_default_cat_by_url' );

Now you can use the parameter post_cat to pass a comma separated list of category slugs:

enter image description here

See also:

  • Template plugin for blog posts?
  • Open Wordpress 'Add New Post' admin page with parameters set via $_GET
  • Force category choice before creating new post?

Dave James Miller over at GitHub nailed this one for me. None of the work is from me, I'm just posting his code wrapped into a plguin since it works perfectly as advertised:

<?php
/**
 * Plugin Name: Set default category from url parameter
 * Plugin URI:  https://gist.github.com/davejamesmiller/1966543
 * Description: enables you to setup new post links with the post_title, category and tags in the url: <code><a href="<?= esc_attr(admin_url('post-new.php?post_title=Default+title&category=category1&tags=tag1,tag2')) ?>">New post</a></code>
 * Version:     0.0.1
 * Author:      davejamesmiller
 * Author URI:  https://gist.github.com/davejamesmiller
 */

// I used this code to automatically set the default post title, category and
// tags for a new WordPress post based on which link was clicked. It could also
// be tweaked to hard-code the values instead of using request parameters.


add_filter('wp_get_object_terms', function($terms, $object_ids, $taxonomies, $args)
{
    if (!$terms && basename($_SERVER['PHP_SELF']) == 'post-new.php') {

        // Category - note: only 1 category is supported currently
        if ($taxonomies == "'category'" && isset($_REQUEST['category'])) {
            $id = get_cat_id($_REQUEST['category']);
            if ($id) {
                return array($id);
            }
        }

        // Tags
        if ($taxonomies == "'post_tag'" && isset($_REQUEST['tags'])) {
            $tags = $_REQUEST['tags'];
            $tags = is_array($tags) ? $tags : explode( ',', trim($tags, " \n\t\r\0\x0B,") );
            $term_ids = array();
            foreach ($tags as $term) {
                if ( !$term_info = term_exists($term, 'post_tag') ) {
                    // Skip if a non-existent term ID is passed.
                    if ( is_int($term) )
                        continue;
                    $term_info = wp_insert_term($term, 'post_tag');
                }
                $term_ids[] = $term_info['term_id'];
            }
            return $term_ids;
        }
    }
    return $terms;
}, 10, 4);