phpwordpresspluginscustom-post-typewordpress-capabilities

Wordpress how to change capabilities of a custom post type created by a plugin?


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:

Result in Members Plugin

Something I didn't expect to see and don't understand:

Result in Memebers Plugin

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.


Solution

  • You're on the right track!

    "I thought about de-registering and re-registering the CPT from WPCargo myself"

    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" type capabilities. This is the wordpress default behavior.

    If you go to the following path:

    your website folder > wp-content > plugins > wpcargo > admin > classes

    And open up class-wpc-post-types.php file, on line 43 you see WPCargo uses 'capability_type' => 'post' argument to register its post type (i.e wpcargo_shipment). This will make wpcargo_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 the wpcargo_shipment custom post type too!

    enter image description here

    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

    1. You could use register_post_type_args filter hook, to add a custom capability type which will have its own capabilities tab. The following code goes into your functions.php file of your theme.
    add_filter('register_post_type_args', 'editing_wpcargo_shipment_capability', 10, 2);
    
    function editing_wpcargo_shipment_capability($args, $post_type)
    {
      if ('wpcargo_shipment' === $post_type) {
        $args['map_meta_cap'] = true;
        $args['capability_type'] = 'your_custom_type'; // You could set your capability type name here
      }
      return $args;
    }
    

    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:

    enter image description here

    Now you have a separate tab for your custom post type capabilities! :-)


    2nd way, NOT recommended

    You could navigate to the following path:

    your website folder > wp-content > plugins > wpcargo > admin > classes

    And open up class-wpc-post-types.php file, on line 43.

    Replace:

    'capability_type' => 'post'
    

    with:

    'capability_type' => 'your_custom_type',
    'map_meta_cap'    => true,
    

    Which will basically give you the same thing:

    enter image description here

    Note:


    Also worth mentioning, in both solutions "custom" tab will stay intact with NO extra stuff!

    enter image description here