Problem using role_has_cap hook

I’m trying to filter a capability check in order to work around the lack of filters to customize the user edit page in the admin area. But I can’t seem to add filters to a hook, and I’m sure I’m missing something basic here.

The hook in question is role_has_cap, located at line 343 of wp-includes/capabilities.php. It reads:

Read More
$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );

I’m trying to test my filter by adding this to my functions.php:

add_filter('role_has_cap', 'my_role_has_cap_filter', 10, 3);
function my_role_has_cap_filter($var_one, $var_two, $var_three) {
    print_r($var_one);
    print_r($var_two);
    print_r($var_three);
}

But absolutely nothing happens. Even if I add the filter with less parameters than my function requires (which, in my limited knowledge, should trigger a PHP error) I’m completely ignored by WordPress.

What I’m actually trying to achieve is returning false to a current_user_can( 'edit_posts' ) or current_user_can( 'edit_pages' ) when viewing user-edit.php on the admin area, without actually removing those capabilities, but the fact that a filter is not applying seems to me more relevant that my ultimate goal.

Related posts

Leave a Reply

1 comment

  1. I finally figured it out:

    To start with, I was using the wrong hook. I should have been using user_has_cap hook instead, which is what actually has a chance of being called when using current_user_can().

    But second, and most important of all, I was seeing the page while logged-in as super_admin, which didn’t trigger the apply_filter function at all. Since by definition the super_admin has all capabilities, WP doesn’t feel the need to filter any of the checks.