I've tried to make a script that only allows a specific user to edit a specific page but without any success.
The idea behind, is that if a users username contains a specific word, they are allowed to edit a special page.
I've tried to do this, by granting the specific users a special capability, and then checking that capability when opening the special page.
Here's my code:
add_action( 'admin_init', function() {
$current_user = wp_get_current_user();
$user_login = $current_user->data->user_login;
if( strpos($user_login, '@special-domain.com') !== false ) {
$user = new WP_User($current_user->ID);
$user->add_cap('edit_special_page');
}
});
add_filter( 'map_meta_cap', function( $caps, $cap, $user_id, $args ) {
if ( 'edit_page' == $cap && user_can( $user_id, 'edit_special_page' ) )
{
$post_id = $args[0];
$special_page_id = get_page_by_path('special-page') ? get_page_by_path('special-page')->ID : null;
if ( special_page_id == $post_id ) {
$caps = [];
$caps[] = 'edit_special_page';
}
}
return $caps;
}, 10, 4 );
But all users can still edit to the special page, which should only be possible for users with @special-domain.com in their usernames.
I've also tried to use the post-id directly instead of $special_page_id
, but with the same result.
Any idea why this is not working and how to do it?
PS: I am NOT interested in any kind of plugins.