zMastaa.com

A geek at home. A web and mobile Apps Developer at Bella Design in Cheshire. A man united fan all day long.

Modifying existing WordPress post types / Removing Add New post button

November 20th, 2015

 

I hit an issue with an existing event plugin on a client’s website that created a post type but didn’t distinguish “edit posts” and “create new post” capabilities

 

This is the original capability mapping of the post type “event”,  you will notice create_posts and edit_posts are mapped to the same custom user role.

9/10 times this good enough for most use cases, I managed to come across that rare scenario were i needed to separate the two capabilities.

So how do you modify the custom roles of an existing post type? It ended up being relatively simple.

Using WordPress’ get_post_type_object function to load up the existing post type you can see the existing setup.

The output will show the post type’s mapped capabilities under the cap object array, with this knowledge you can modify the variable you’re after then initiate the changes using the wordpress add_action function.

For example

 

Now in a user role editor you should see the following, any user not granted the create_events (create_posts) capability will no longer be able see the Add New button.

Wordpress post type capabilities

 

The new cap output