GitHub的Dave James Miller帮我搞定了这个。这些工作都不是我做的,我只是把他的代码打包在一个plguin中,因为它和广告中的一样完美:
<?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);