2

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:

$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.

8
  • Do you have WP_DEBUG set to TRUE? Further: Try to wrap your filter call inside a function that you hook during init.
    – kaiser
    Feb 21, 2012 at 12:37
  • I do have WP_DEBUG set to TRUE. I've wrapped it around a function on init, but still get nothing. Feb 21, 2012 at 12:49
  • Oh. You always need to return the 1st argument in the list.
    – kaiser
    Feb 21, 2012 at 12:51
  • Plus: The filter should only work if you're actually questioning if some role has a cap in one of your templates (for example). Else the fn won't get triggered.
    – kaiser
    Feb 21, 2012 at 12:52
  • 1
    The filter is inside a function that returns true or false, it won't produce output, not in the way it's used. If you want to dump that data, just do it inside the core file directly(wp-includes/capabilities.php), then undo the changes when you're done seeing what you need to see.
    – t31os
    Feb 21, 2012 at 13:11

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.