1

I'm looking for a way to allow users with an assigned WordPress role (in my case, author) to create new categories and then automatically be restricted to only posting in those categories or the default 'uncategorized.'

Essentially, I'm creating a story-building tool where users can add new 'stories' or 'series' (I have renamed the 'post' and 'category' labels) so they need to be able to create a new 'series' that only they can add to.

I have tried numerous plugins that restrict posting to within certain categories, but they all require an admin to set permissions and I need permissions to be assigned automatically upon creation of the category.

(In case it's relevant, I'm not using BuddyPress, and I would prefer not to go multisite if possible).

Thanks!

2
  • did not get your question Feb 10, 2016 at 17:10
  • You might be able to hook into create_term or a similar hook - save the author ID as term_meta and base your conditionals off that. I'm not sure if it's the best solution but it's a starting point.
    – Howdy_McGee
    Feb 10, 2016 at 17:15

1 Answer 1

1

This little plugin shows the user role 'author' only its own categories & posts. So you can edit it for your own post-type and taxonomy.

/*
Plugin Name: Show own categories
Description: This plugin shows the user role 'author' only its own categories & posts
Version:     0.1
Author:      Soren Wrede
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

class SW_Category_Restriction{
    private $user_cats = NULL;

    public function __construct(){
        //Save author ID for category
        add_action( 'create_category', array( &$this, 'save_category_author' ) );
        //Set manage_categories cap for 'Author'
        add_action( 'admin_init', array( &$this, 'add_author_cap_categories' ) );
        //Remove manage_categories  capfor 'Author'
        register_deactivation_hook( __FILE__, array( &$this, 'remove_author_cap_categories' ) );
        //Filter categorys in new-post, edit-post, edit-categories
        add_action( 'admin_print_scripts-post-new.php', array( &$this, 'filter_post_page' ) );
        add_action( 'admin_print_scripts-post.php', array( &$this, 'filter_post_page' ) );
        add_action( 'admin_print_scripts-edit-tags.php', array( &$this, 'filter_post_page' ) );
        //just show own posts
        global $pagenow;
        if ( $pagenow == 'edit.php' )
            add_filter( 'pre_get_posts', array( &$this, 'filter_edit_page' ) );
    }

    public function save_category_author( $term_id ) {
        $user_id = get_current_user_id();
        add_term_meta( $term_id, 'author', $user_id );
    }

    public function get_user_cats( $user_id ) {
        $args = [
            'hide_empty' => false,
            'meta_query' => [
                [
                    'key' => 'author',
                    'value' => $user_id,
                ]
            ]
        ];
        $terms = get_terms( 'category', $args );
        $ids = '-1,';
        foreach ($terms as $term ) {
            $ids .= $term->term_id . ',';
        }
        $ids = substr($ids, 0, -1);
        $this->user_cats = $ids;
    }

    public function add_author_cap_categories() {
        $role = get_role( 'author' );
        $role->add_cap( 'manage_categories' );
    }

    public function remove_author_cap_categories() {
        $role = get_role( 'author' );
        $role->remove_cap( 'manage_categories' );
    }

    public function exclusions( $exclusions ){
        $exclusions .= " AND ( t.term_id IN ( $this->user_cats ) OR tt.taxonomy NOT IN ( 'category' ) )";
        return $exclusions;
    }

    public function filter_post_page() {
        //check post-type 
        $screen = get_current_screen();
        if ( $screen->post_type != 'post' )
            return;

        //just Author and lower
        if ( current_user_can('delete_others_posts') )
            return;

        $user_id = get_current_user_id();
        $this->get_user_cats( $user_id );

        if ( empty( $this->user_cats ) )
            return;

        add_filter( 'list_terms_exclusions', array( &$this, 'exclusions' ) );
    }

    public function filter_edit_page( $query ) {
        if ( current_user_can('delete_others_posts') )
            return;     
        $query->set( 'author', get_current_user_id() );

        return $query;
    }

}

new SW_Category_Restriction();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.