Support » Plugin: Custom Post Type UI » Add capabilities to taxonomies

  • Resolved sketchuparchive

    (@sketchuparchive)


    Hello,
    Firstly, thanks a lot for your plugin (CPT UI),it works great ,but when I installed WC vendors and I can not assign product to the custom taxonomy I made with your Plugin(when I login as a vendor,every thing is fine for admin user), The Author of WC vendors say that I must add Add capabilities to the Custom taxonomies I made,

    I hope You can Help me,
    Thanks in advance

    https://wordpress.org/plugins/custom-post-type-ui/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    We have the following filter available right before the registration of each taxonomy.

    /**
     * Filters the arguments used for a taxonomy right before registering.
     *
     * @since 1.0.0
     * @since 1.3.0 Added original passed in values array
     *
     * @param array  $args     Array of arguments to use for registering taxonomy.
     * @param string $value    Taxonomy slug to be registered.
     * @param array  $taxonomy Original passed in values for taxonomy.
     */
    $args = apply_filters( 'cptui_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy );
    
    return register_taxonomy( $taxonomy['name'], $object_type, $args );

    You could set up a filter that adds to the $args array and adds in the necessary capabilities.

    We don’t have a 100% match with the plugin at the moment, and capability stuff is one of the bigger missing pieces.

    Thread Starter sketchuparchive

    (@sketchuparchive)

    Hi Michael,
    Thanks for the quick reply, I’m new to coding, I saw this post :
    Here,
    I will be thankful if you tell me how exactly the code should look like after adding capabilities, because every time I added it my website is crashed.

    regards,

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    The post at https://wordpress.org/support/topic/capabilities-for-custom-taxonomies?replies=4#post-6964304 is pretty much what I’d type up now.

    Perhaps determining what the error is that you’re encountering would be best here.

    Thread Starter sketchuparchive

    (@sketchuparchive)

    Sorry for my Silly question, I am an Architect this is the first time I do these things, I put it like this :

    function gaseous_taxonomy_caps( $args, $taxonomy ) {
    	$args['capabilities'] = array(
    		'edit_terms'   => 'edit_' . $taxonomy['name'],
    		'delete_terms' => 'delete_' . $taxonomy['name'],
    		'assign_terms' => 'assign_' . $taxonomy['name'],
    	);
    
    	return $args;
    }
    add_filter( 'cptui_pre_register_taxonomy', 'gaseous_taxonomy_caps', 10, 2 );
    
    $args = apply_filters( 'cptui_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy );
    
    return register_taxonomy( $taxonomy['name'], $object_type, $args );

    I now that is wrong,

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Correct, that’s not quite what you need. I was simply showing you the filter in my first reply.

    The following is all that you would need to add to your functions.php file, in order to set those capabilities on the taxonomies:

    function gaseous_taxonomy_caps( $args, $taxonomy ) {
    	$args['capabilities'] = array(
    		'edit_terms'   => 'edit_' . $taxonomy['name'],
    		'delete_terms' => 'delete_' . $taxonomy['name'],
    		'assign_terms' => 'assign_' . $taxonomy['name'],
    	);
    
    	return $args;
    }
    add_filter( 'cptui_pre_register_taxonomy', 'gaseous_taxonomy_caps', 10, 2 );
    Thread Starter sketchuparchive

    (@sketchuparchive)

    Hi,
    Thanks for that explanation,I do that, From where I can add capabilities for Taxonomies which have “Attach to Post Type: products” ?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Couple things.

    1. I noticed I have my snippet wrong sightly above. Instead of $taxonomy['name'] it should just be $taxonomy. That second parameter passed in is a string, not an array. Sorry about that.

    2. As is, this is doing the capabilities assignment for all taxonomies you’re registering. If you only want this to be for specific ones, you can wrap the $args['capabilities'] assignment in an if statement and check what the $taxonomy variable holds. Small case of knowing which taxonomy you’re using for what post type, which would be “products” Revised example snippet below. Replace “SOMETHING” with the taxonomy slug of choice.

    function gaseous_taxonomy_caps( $args, $taxonomy ) {
    	if ( 'SOMETHING' == $taxonomy ) {
    		$args['capabilities'] = array(
    			'edit_terms'   => 'edit_' . $taxonomy,
    			'delete_terms' => 'delete_' . $taxonomy,
    			'assign_terms' => 'assign_' . $taxonomy,
    		);
    	}
    
    	return $args;
    }
    add_filter( 'cptui_pre_register_taxonomy', 'gaseous_taxonomy_caps', 10, 2 );
    Thread Starter sketchuparchive

    (@sketchuparchive)

    Hi Micheal,
    Thanks a lot it is working right now, the general code turn off the capabilities for all users even admin, I am using “User Role editor” to Add the capabilities an turn it on, I have a lot of taxonomies and I want to add the capabilities to three of them, I tried to repeat the code above , but it didn’t work.

    Sorry for asking a lot of questions.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’d amend the code above to the following:

    if ( 'SOMETHING' == $taxonomy || 'SOMETHING_ELSE' == $taxonomy || 'THIRD_THING' == $taxonomy ) { ... }

    It’ll check for any of the 3 and let the code run if any of those 3 match.

    Thread Starter sketchuparchive

    (@sketchuparchive)

    Thanks Micheal, it works like a charm, you are genius 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Add capabilities to taxonomies’ is closed to new replies.