* 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: New post
* 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);