问题是map_meta_cap
将授予所有用户执行该操作的权限,因为这与“不需要任何功能”的说法基本相同。你真正想做的就是添加edit_special_page
现有所需功能列表:
add_filter( \'map_meta_cap\', function( $caps, $cap, $user_id, $args ) {
if ( \'edit_post\' === $cap || \'edit_page\' === $cap ) {
$current_page_id = $args[0];
$special_page_id = get_page_by_path( \'special-page\' ) ? get_page_by_path( \'special-page\' )->ID : null;
// If the page being edited is the special page...
if ( $special_page_id === $current_page_id ) {
// ... the user must have the edit_special_page capability.
$caps[] = \'edit_special_page\';
}
}
return $caps;
}, 10, 4 );
一个不相关的问题,但您需要解决的是,您不应该在上面添加功能
admin_init
或
init
. 添加功能会写入数据库,因此只有在插件/主题激活或用户注册时才能执行以下操作:
add_action( \'user_register\', function( $user_id ) {
$user = get_userdata( $user_id );
if ( strpos( $user->user_login, \'@special-domain.com\' ) !== false ) {
$user->add_cap( \'edit_special_page\' );
}
} );