new+post.php,如何在“ wp-admin / post-new.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: 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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值