如果你是一個 WordPress 開發者,給自己的日志(或者自定義類型的日志)添加自定義分類模式(custom taxonomy),并且你的系統還支持注冊用戶在前臺通過一個表單來投稿,并且需要用戶也能輸入自定義分類,這個時候你就使用 wp_insert_post
函數來插入日志,但是 wp_insert_post
函數內部是有權限判斷的:
if ( current_user_can($taxonomy_obj->cap->assign_terms) ) wp_set_post_terms( $post_ID, $tags, $taxonomy );
自定義分類模式(custom taxonomy)默認的 assign_terms
權限是:manage_categories
,可以管理分類,而只有管理員或者編輯(editor)可以管理分類。所以我們在創建自定義分類的時候,就要將其 assign_terms
權限設置為支持訂閱者。比如:
register_taxonomy( 'device', 'post', array( 'hierarchical' => true, 'label' => '適用設備', 'query_var' => true, 'rewrite' => array('slug' => 'device','with_front'=>false), 'capabilities' => array( 'manage_terms' => 'manage_categories', 'edit_terms' => 'manage_categories', 'delete_terms' => 'manage_categories', 'assign_terms' => 'read' ) ));
上面就創建了一個 “device” 的自定義分類,并且將其權限分派設置為 read,這樣訂閱者(普通用戶)也能操作了。