I'm looking to edit a CPT (custom post type) that a plugin has generated within WordPress, but I'm unsure how to go about it properly. The plugin I'm using that has created the CPT is WPCargo. The name of the CPT is 'wpcargo_shipment'.
My reason for editing this post type is I need more granular control over the user permissions of this plugin. I'm looking to restrict access to all other parts of WP in the backend except for WPCargo's CPT.
I'm attempting to use the Members Plugin by Memberpress to control my user permissions. I've found this article here, which talks about tweaking a post type for the Members Plugin as it's being created but not after the fact.
Here's my attempt at editing the CPT, but my attempt appears somewhat work, but not entirely.
/* Edit (wpcargo_shipment) cpt */
function edit_wpcargo_shipment_capability( $args, $post_type ) {
if ( 'wpcargo_shipment' === $post_type ) {
$args['slug'] = $slug; // get and store slug for later use.
$slug_plural = $slug . 's'; // define slug as plural & store for later use.
$args['map_meta_cap'] = true;
$args['capability_type'] = $slug;
$args['capabilities'] = [ // this is the part im most uncertain of.
'create_posts' => 'create_' . $slug_plural,
'delete_others_posts' => 'delete_others_' . $slug_plural,
'delete_posts' => 'delete_' . $slug_plural,
'delete_private_posts' => 'delete_private_' . $slug_plural,
'delete_published_posts' => 'delete_published_' . $slug_plural,
'edit_posts' => 'edit_' . $slug_plural,
'edit_others_posts' => 'edit_others_' . $slug_plural,
'edit_private_posts' => 'edit_private_' . $slug_plural,
'edit_published_posts' => 'edit_published_' . $slug_plural,
'publish_posts' => 'publish_' . $slug_plural,
'read_private_posts' => 'read_private_' . $slug_plural,
'read' => 'read',
];
}
return $args;
}
add_filter( 'register_post_type_args', 'edit_wpcargo_shipment_capability', 10, 2 );
The Result:
Something I didn't expect to see and don't understand:
Question:
it's been generated, and if you can (if I should), how do you do it?I want to know if it is possible to edit a CPT's args after
Note:
I thought about de-registering and re-registering the CPT from WPCargo myself but wanted to see if a more straightforward solution was possible first.
You're on the right track!
NO NEED for de-registering and re-registering. It's absolutely feasible after you install and activate your plugin.
So here's what's happening, when "WPCargo" registers its custom post type, it uses "post" type as
capability_type
.What does that mean? Well, it means
wpcargo_shipment
post type will inherit all of the default "post" typecapabilities
. This is the wordpress default behavior.If you go to the following path:
And open up
class-wpc-post-types.php
file, on line43
you seeWPCargo
uses'capability_type' => 'post'
argument to register its post type (i.e wpcargo_shipment). This will makewpcargo_shipment
custom post type to inherit default "post" capabilities, which means, on the "members" plugin you have control over the "post" type capabilities. What happens to the "post" capabilities, will happen to thewpcargo_shipment
custom post type too!As you can see, there is no custom post type capability tab for "Shipment".
BUT you want to have a specific tab for your "Shipment" custom post type right? We'll change it soon!
Well, there are two ways you could do that:
1st way, recommended way
register_post_type_args
filter hook, to add a custom capability type which will have its own capabilities tab. The following code goes into yourfunctions.php
file of your theme.Which will give you a new capability type named "your_custom_type", Which will show up on your "members" plugin, which will give you all of the wordpress capabilities!
Note:
map_meta_cap
toTRUE
will do the magic here!your_custom_type
as yourcapability_type
, just to give you an example, feel free to change it!Now you have a separate tab for your custom post type capabilities! :-)
2nd way, NOT recommended
You could navigate to the following path:
And open up
class-wpc-post-types.php
file, on line43
.Replace:
with:
Which will basically give you the same thing:
Note:
Also worth mentioning, in both solutions "custom" tab will stay intact with NO extra stuff!