Support » Plugins » Hacks » custom post type with categories and tags – checkbox disabled

  • Hi everyone,

    I’m having a small problem with a plugin i’ve created. I’m trying to give the users permission to add categories and tags to the custom post type but the tag box is half loaded and the categories box checkboxes are disabled. After some test I’ve found that I need to give the users the permission to edit_posts in order to get this working but I cannot do this. This is the code I used to create the custom post. Is there any way to remap that permission to my own capability ?

    
    function liens_pc_create_post_type() {
    	
    	// Capacités du plugin (Permissions)
    	$capabilities = array(
    		 'publish_posts' => 'publish_lienpcs',
    		 'edit_posts' => 'edit_lienpcs',
    		 'edit_others_posts' => 'edit_others_lienpcs',
    		 'delete_posts' => 'delete_lienpcs',
    		 'delete_others_posts' => 'delete_others_lienpcs',
    		 'read_private_posts' => 'read_private_lienpcs',
    		 'edit_post' => 'edit_lienpc',
    		 'delete_post' => 'delete_lienpc',
    		 'read_post' => 'read_lienpc'
    		
    	);
    
    	
    	// Étiquettes du plugin
    	$labels = array(
    		'name' => 'Bibliothèque de liens',
    		'singular_name' => 'Lien',
    		'add_new' => 'Ajouter un lien ',
    		'add_new_item' => 'Ajouter un lien',
    		'edit_item' => 'Modifier le lien',
    		'new_item' => 'Nouveau lien',
    		'all_items' => 'Tous les liens',
    		'view_item' => 'Voir le lien',
    		'search_items' => 'Trouver le lien',
    		'not_found' =>  'Aucun lien(s) trouvé',
    		'not_found_in_trash' => 'Aucun lien trouvé dans la corbeille', 
    		'parent_item_colon' => '',
    		'menu_name' => 'Bibliothèque de Liens',
    	);
    	
    	
    	//register post type
    	register_post_type( 
    		    'liens_pc', array(
    			'labels' => $labels,
    			'has_archive' => true,
    			'public' => true,
    			'map_meta_cap' => true,
    			'supports' => array( 'title', 'author' ),
    			'exclude_from_search' => false,
    			'capability_type' => 'page',
    			'capabilities' => $capabilities,
    			'rewrite' => array( 'slug' => 'liens_pc' ),
    			'taxonomies' => array('category','post_tag'),
    			//'register_meta_box_cb'=> 'liens_pc_load_scripts_add'
    		)
    	);
    
    	flush_rewrite_rules(false);
    }
    

    Any help would be appreciated . Thanks.

    • This topic was modified 2 years, 7 months ago by  Hyrules.
    • This topic was modified 2 years, 7 months ago by  Hyrules.
    • This topic was modified 2 years, 7 months ago by  Hyrules.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Add this after your register_post_type() call:

    global $wp_taxonomies;
    $wp_taxonomies['category']->cap->assign_terms = 'edit_lienpcs';
    $wp_taxonomies['post_tag']->cap->assign_terms = 'edit_lienpcs';

    While this will fix the issue with your CPT, you will now have a similar problem with posts. To work in both cases, assign “manage_categories” instead. This would mean someone who should not be able to edit posts or lienpcs would be able to add category or tag terms to such posts. Doing so without access to the edit screen would be difficult, so I think you’re safe, but theoretically speaking it’s a problem. Of course the user would need “manage_categories” capability for this to work.

    A better solution would be to dynamically assign capability based on the post type of the post being edited.

    The best solution would be for this bug to be fixed in core. I do believe this to be a bug, and have filed a Trac ticket about it. You may want to subscribe the the linked ticket for updates on addressing the bug.

    BTW, you shouldn’t call flush_rewrite_rules() from a callback the runs on every page load, it’s an expensive operation and is unnecessary to do that often. It only needs to be called when rewrite rules are actually changed. It’s often called only when a plugin that changes rewrite rules is activated. You can manually flush the rules by going to Settings > Permalinks and clicking Save Changes. You don’t need to actually change anything.

    You also don’t need 'capability_type' => 'page', when you are providing a capabilities array.

    Wow thanks alot. That fixed my issue. I will subscribe to the bug as you said if this or I can help in anyway.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom post type with categories and tags – checkbox disabled’ is closed to new replies.