1

我正在寻找编辑插件在 WordPress 中生成的 CPT(自定义帖子类型),但我不确定如何正确进行。我使用的创建 CPT 的插件是 WPCargo。CPT 的名称是“wpcargo_shipment”。

我编辑这个帖子类型的原因是我需要更精细地控制这个插件的用户权限。我希望限制对后端 WP 的所有其他部分的访问,除了 WPCargo 的 CPT。

我正在尝试使用Memberpress的成员插件来控制我的用户权限。我在这里找到了这篇文章,它讨论了在创建成员插件时调整帖子类​​型,但不是在事后。

这是我编辑 CPT 的尝试,但我的尝试似乎有些奏效,但并不完全。

/* 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 );

结果:

结果成员插件

我没想到会看到并且不理解的东西:

结果在成员插件中

问题:

它已经生成了,如果可以(如果我应该),你是怎么做的?我想知道是否可以在之后编辑 CPT 的参数

笔记:

我曾想过自己从 WPCargo 注销和重新注册 CPT,但想先看看是否有更直接的解决方案。

4

1 回答 1

3

你在正确的轨道上!

“我想过自己从 WPCargo 注销和重新注册 CPT”

无需注销和重新注册。安装并激活插件后绝对可行。

所以这就是发生的事情,当“WPCargo”注册其自定义帖子类型时,它使用“post”类型作为capability_type.

这意味着什么?好吧,这意味着wpcargo_shipment帖子类型将继承所有默认的“帖子”类型capabilities。这是 wordpress 的默认行为。

如果您转到以下路径:

您的网站文件夹 > wp-content > plugins > wpcargo > admin > classes

并打开class-wpc-post-types.php文件,您会在网上43看到WPCargo使用'capability_type' => 'post'参数来注册其帖子类型(即 wpcargo_shipment)。这将使wpcargo_shipment自定义帖子类型继承默认的“帖子”功能,这意味着,在“成员”插件上,您可以控制“帖子”类型的功能。“发布”功能会发生什么,wpcargo_shipment自定义帖子类型也会发生什么!

在此处输入图像描述

如您所见,“发货”没有自定义帖子类型功能选项卡。

但是您想为您的“装运”自定义帖子类型设置一个特定选项卡吗?我们很快就会改变它!


好吧,有两种方法可以做到这一点:

第一种方式,推荐方式

  1. 您可以使用register_post_type_args过滤器挂钩来添加自定义功能类型,该类型将具有自己的功能选项卡。以下代码进入您functions.php的主题文件。
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;
}

这将为您提供一个名为“your_custom_type”的新功能类型,它将显示在您的“members”插件上,它将为您提供所有 wordpress 功能!

笔记:

  • 设置map_meta_capTRUE将在这里发挥作用!
  • 我用your_custom_type你的capability_type,只是给你一个例子,随意改变它!

在此处输入图像描述

现在,您有一个单独的选项卡用于自定义帖子类型功能!:-)


第二种方式,不推荐

您可以导航到以下路径:

您的网站文件夹 > wp-content > plugins > wpcargo > admin > classes

并打开class-wpc-post-types.php文件,在线43

代替:

'capability_type' => 'post'

和:

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

这基本上会给你同样的东西:

在此处输入图像描述

笔记:

  • 不推荐使用第二种解决方案,因为我们正在操作该插件核心文件。如果您想这样做,请密切关注插件的未来更新,并确保您的更改保持不变!

另外值得一提的是,在这两种解决方案中,“自定义”选项卡都将保持不变,没有额外的东西!

在此处输入图像描述

于 2021-09-29T23:23:30.810 回答