Wordpress 如何更改插件创建的自定义 post 类型的功能?
Wordpress how to change capabilities of a custom post type created by a plugin?
我正在寻找编辑插件在 WordPress 中生成的 CPT(自定义 post 类型),但我不确定如何正确进行。我使用的创建 CPT 的插件是 WPCargo。 CPT 的名称是 'wpcargo_shipment'.
我编辑此 post 类型的原因是我需要更精细地控制此插件的用户权限。我希望在后端限制对 WP 的所有其他部分的访问,WPCargo 的 CPT 除外。
我正在尝试使用 Members Plugin by Memberpress to control my user permissions. I've found this article here,它讨论了在创建成员插件时为成员插件调整 post 类型,而不是事后。
这是我编辑 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的args
注:
我考虑过自己从 WPCargo 注销和重新注册 CPT,但想先看看是否有更直接的解决方案。
你走对了!
"I thought about de-registering and re-registering the CPT from WPCargo myself"
不需要注销和重新注册。安装并激活插件后绝对可行。
这就是发生的事情,当“WPCargo”注册其自定义 post 类型时,它使用“post”类型作为 capability_type
.
这是什么意思?那么,这意味着 wpcargo_shipment
post 类型将继承所有默认的“post”类型 capabilities
。这是 wordpress 默认行为。
如果您转到以下路径:
your website folder > wp-content > plugins > wpcargo > admin > classes
然后打开 class-wpc-post-types.php
文件,在行 43
你看到 WPCargo
使用 'capability_type' => 'post'
参数来注册它的 post 类型(即 wpcargo_shipment).这将使 wpcargo_shipment
自定义 post 类型继承默认的 "post" 功能,这意味着,在 "members" 插件上你可以控制 "post" 类型的功能. “post”功能会发生什么,wpcargo_shipment
自定义 post 类型也会发生什么!
如您所见,“发货”没有自定义 post 类型功能选项卡。
但是 您想为您的“货件”自定义 post 类型设置一个特定的选项卡吗?我们会尽快更改!
嗯,有两种方法可以做到这一点:
第一种方式,推荐方式
- 您可以使用
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”的新功能类型,它将显示在您的“成员”插件中,这将为您提供所有 wordpress 功能!
注:
- 将
map_meta_cap
设置为 TRUE
将发挥神奇作用!
- 我用
your_custom_type
作为你的capability_type
,只是给你举个例子,你可以随意更改!
现在您的自定义 post 类型功能有了一个单独的选项卡! :-)
第二种方式,不推荐
您可以导航到以下路径:
your website folder > wp-content > plugins > wpcargo > admin > classes
并打开 class-wpc-post-types.php
文件,第 43
.
行
替换:
'capability_type' => 'post'
与:
'capability_type' => 'your_custom_type',
'map_meta_cap' => true,
这基本上会给你同样的东西:
注:
- 不推荐第二种解决方案,因为我们正在操纵该插件核心文件。如果你想这样做,那么请密切关注插件的未来更新,并确保你的更改完好无损!
另外值得一提的是,在这两种解决方案中,“自定义”选项卡都将保持原样,没有多余的东西!
我正在寻找编辑插件在 WordPress 中生成的 CPT(自定义 post 类型),但我不确定如何正确进行。我使用的创建 CPT 的插件是 WPCargo。 CPT 的名称是 'wpcargo_shipment'.
我编辑此 post 类型的原因是我需要更精细地控制此插件的用户权限。我希望在后端限制对 WP 的所有其他部分的访问,WPCargo 的 CPT 除外。
我正在尝试使用 Members Plugin by Memberpress to control my user permissions. I've found this article here,它讨论了在创建成员插件时为成员插件调整 post 类型,而不是事后。
这是我编辑 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的args注:
我考虑过自己从 WPCargo 注销和重新注册 CPT,但想先看看是否有更直接的解决方案。
你走对了!
"I thought about de-registering and re-registering the CPT from WPCargo myself"
不需要注销和重新注册。安装并激活插件后绝对可行。
这就是发生的事情,当“WPCargo”注册其自定义 post 类型时,它使用“post”类型作为 capability_type
.
这是什么意思?那么,这意味着 wpcargo_shipment
post 类型将继承所有默认的“post”类型 capabilities
。这是 wordpress 默认行为。
如果您转到以下路径:
your website folder > wp-content > plugins > wpcargo > admin > classes
然后打开 class-wpc-post-types.php
文件,在行 43
你看到 WPCargo
使用 'capability_type' => 'post'
参数来注册它的 post 类型(即 wpcargo_shipment).这将使 wpcargo_shipment
自定义 post 类型继承默认的 "post" 功能,这意味着,在 "members" 插件上你可以控制 "post" 类型的功能. “post”功能会发生什么,wpcargo_shipment
自定义 post 类型也会发生什么!
如您所见,“发货”没有自定义 post 类型功能选项卡。
但是 您想为您的“货件”自定义 post 类型设置一个特定的选项卡吗?我们会尽快更改!
嗯,有两种方法可以做到这一点:
第一种方式,推荐方式
- 您可以使用
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”的新功能类型,它将显示在您的“成员”插件中,这将为您提供所有 wordpress 功能!
注:
- 将
map_meta_cap
设置为TRUE
将发挥神奇作用! - 我用
your_custom_type
作为你的capability_type
,只是给你举个例子,你可以随意更改!
现在您的自定义 post 类型功能有了一个单独的选项卡! :-)
第二种方式,不推荐
您可以导航到以下路径:
your website folder > wp-content > plugins > wpcargo > admin > classes
并打开 class-wpc-post-types.php
文件,第 43
.
替换:
'capability_type' => 'post'
与:
'capability_type' => 'your_custom_type',
'map_meta_cap' => true,
这基本上会给你同样的东西:
注:
- 不推荐第二种解决方案,因为我们正在操纵该插件核心文件。如果你想这样做,那么请密切关注插件的未来更新,并确保你的更改完好无损!
另外值得一提的是,在这两种解决方案中,“自定义”选项卡都将保持原样,没有多余的东西!