Discussion:
Cannot figure this out: user_has_cap
Dino Termini
2014-06-02 16:26:05 UTC
Permalink
Hi list,

For the life of me, I can't figure this out. I want to prevent my
authors to edit a bunch of pages on my website.

Aside from using pre_get_posts to change wp_query, I'd also tweak
current_user_can "on the fly" and remove "edit_pages" from the user's
capabilities, if the ID is in my blacklist.

So I created a plugin with the following code:

function tweak_edit_page_capability( $allcaps, $caps, $args ) {
file_put_contents( '/file/somewhere', 'it works!', FILE_APPEND );
return $allcaps;
}
add_filter( 'user_has_cap', 'tweak_edit_page_capability', 10, 3 );

However the function is not called! What am I doing wrong? I've
deactivated all plugins and reset the theme, but nothing helps!

Dino.
John Blackbourn
2014-06-02 16:52:59 UTC
Permalink
Are you logged in as a super admin on multisite? The user_has_cap filter
doesn't get called in that situation.

My User Switching plugin has a nice bit of documentation with its callback
functions for this filter and the corresponding map_meta_cap filter, which
may help you. See here and here:

https://github.com/johnbillion/user-switching/blob/0.8.8/user-switching.php#L36-L37
https://github.com/johnbillion/user-switching/blob/0.8.8/user-switching.php#L533-L576

John
Post by Dino Termini
Hi list,
For the life of me, I can't figure this out. I want to prevent my authors
to edit a bunch of pages on my website.
Aside from using pre_get_posts to change wp_query, I'd also tweak
current_user_can "on the fly" and remove "edit_pages" from the user's
capabilities, if the ID is in my blacklist.
function tweak_edit_page_capability( $allcaps, $caps, $args ) {
file_put_contents( '/file/somewhere', 'it works!', FILE_APPEND );
return $allcaps;
}
add_filter( 'user_has_cap', 'tweak_edit_page_capability', 10, 3 );
However the function is not called! What am I doing wrong? I've
deactivated all plugins and reset the theme, but nothing helps!
Dino.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dino Termini
2014-06-02 16:55:14 UTC
Permalink
THANK YOU!!! :) I was indeed logged in as a super admin on mu :/
Post by John Blackbourn
Are you logged in as a super admin on multisite? The user_has_cap filter
doesn't get called in that situation.
My User Switching plugin has a nice bit of documentation with its callback
functions for this filter and the corresponding map_meta_cap filter, which
https://github.com/johnbillion/user-switching/blob/0.8.8/user-switching.php#L36-L37
https://github.com/johnbillion/user-switching/blob/0.8.8/user-switching.php#L533-L576
John
Post by Dino Termini
Hi list,
For the life of me, I can't figure this out. I want to prevent my authors
to edit a bunch of pages on my website.
Aside from using pre_get_posts to change wp_query, I'd also tweak
current_user_can "on the fly" and remove "edit_pages" from the user's
capabilities, if the ID is in my blacklist.
function tweak_edit_page_capability( $allcaps, $caps, $args ) {
file_put_contents( '/file/somewhere', 'it works!', FILE_APPEND );
return $allcaps;
}
add_filter( 'user_has_cap', 'tweak_edit_page_capability', 10, 3 );
However the function is not called! What am I doing wrong? I've
deactivated all plugins and reset the theme, but nothing helps!
Dino.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...