如何禁用 Wordpress 管理员中特定页面的删除选项?
How to disable delete option for a specific page in Wordpress admin?
我发现了很多这样的问题,但没有一个符合我的要求 requirement.Here 我的要求是锁定一个名为设置的特定页面。它不会被 others.But 删除,它应该可以编辑。有什么方法可以使用页面 ID 或名称来锁定特定页面。
按照以下在主题函数文件中创建一个挂钩:
function restrict_page_deletion($post_ID){
$user = get_current_user_id();
$restricted_pageId = 4;
if($post_ID == $restricted_pageId)
{
echo "You are not authorized to delete this page.";
exit;
}
}
add_action('before_delete_post', 'restrict_page_deletion', 10, 1);
将您的页面 ID 传递给 restricted_pageId 变量。
如果您想为多个页面实现此功能,请使用数组代替变量。
管理员可以将页面移至回收站,但管理员无法将其删除。
如果您想阻止管理员使用 trach 功能,请在 "wp_trash_post" 操作上调用挂钩。
add_action('wp_trash_post', 'restrict_page_deletion', 10, 1);
function wpse_312694_restrict_page_deletion( $caps, $cap, $user_id, $args ) {
$post_id = $args[0];
if ( $cap === 'delete_post' && $post_id === 117 ) {
$caps[] = 'do_not_allow';
}
return $caps;
}
add_filter( 'map_meta_cap', 'wpse_312694_restrict_page_deletion', 10, 4 );
我发现了很多这样的问题,但没有一个符合我的要求 requirement.Here 我的要求是锁定一个名为设置的特定页面。它不会被 others.But 删除,它应该可以编辑。有什么方法可以使用页面 ID 或名称来锁定特定页面。
按照以下在主题函数文件中创建一个挂钩:
function restrict_page_deletion($post_ID){
$user = get_current_user_id();
$restricted_pageId = 4;
if($post_ID == $restricted_pageId)
{
echo "You are not authorized to delete this page.";
exit;
}
}
add_action('before_delete_post', 'restrict_page_deletion', 10, 1);
将您的页面 ID 传递给 restricted_pageId 变量。
如果您想为多个页面实现此功能,请使用数组代替变量。
管理员可以将页面移至回收站,但管理员无法将其删除。
如果您想阻止管理员使用 trach 功能,请在 "wp_trash_post" 操作上调用挂钩。
add_action('wp_trash_post', 'restrict_page_deletion', 10, 1);
function wpse_312694_restrict_page_deletion( $caps, $cap, $user_id, $args ) {
$post_id = $args[0];
if ( $cap === 'delete_post' && $post_id === 117 ) {
$caps[] = 'do_not_allow';
}
return $caps;
}
add_filter( 'map_meta_cap', 'wpse_312694_restrict_page_deletion', 10, 4 );