如果您是指用户列表上的删除按钮,则要禁用删除选项/wp-admin/users.php
您可以使用user\\u row\\u actions类隐藏带有以下代码段的“delete”链接:
add_filter(
\'user_row_actions\',
function($actions, $user_object) {
$result = new WP_Query(
array(
\'author\'=> $user_object->ID,
\'post_type\'=>\'portfolio\',
\'posts_per_page\'=>1,
)
);
if ( count($result->posts) !== 0 ){
unset($actions[\'delete\']);
}
return $actions;
},
1,2
);
上面的代码片段只是隐藏了链接。它不会阻止访问删除屏幕。为此,我们需要更多的代码:
add_action(
\'load-users.php\',
function() {
if (isset($_GET[\'user\'])) {
$user_object = get_userdata($_GET[\'user\']);
$result = new WP_Query(
array(
\'author\'=> $user_object->ID,
\'post_type\'=>\'portfolio\',
\'posts_per_page\'=>1,
)
);
if( count($result->posts) !== 0 ){
wp_die(\'This user cannot be deleted\');
}
}
}
);
希望这对你有帮助!