0

I am receiving this error when trying to add new capabilities to two of my user roles:

Error: Object of class WP_Role could not be converted to string ... on line 67

The capability I am trying to add will allow the user to see a custom page that we have built in our WP setup. This is the code I have written to populate the roles:

public static function add_csv_capability():void {
        $rolesToPopulate = array((get_role( 'administrator' )), (get_role( 'editor' )));
        $roles = array_fill_keys(($rolesToPopulate),NULL);
        echo $roles;
        
        foreach ($roles as $role) {
            if ($role) {
                $role->add_cap('use_csv_uploader');
            }
        }
    }

Someone asked if this would answer my question, and it does and it doesn't. It explains what the error is, but now that I know what the error is all about, I need to figure out how to get this code to do what I want, which is to allow me to add capabilities to more than one role.

Any help you can give would be greatly appreciated.

Thanks!

  • You can't `echo $roles;`, `$roles` is not a string. I'm guessing you're just checking it for development, so use `var_dump($roles);` instead – aynber Feb 17 '23 at 20:28
  • Welcome to Stack Overflow! It looks like you are `echoing` the WP_Role object here: `echo $roles`. If you need to see what's in the `role` object, use `var_dump($roles)` or `echo '
    ' . print_r($roles, TRUE) . '
    ';`
    – disinfor Feb 17 '23 at 20:30
  • 1
    Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – disinfor Feb 17 '23 at 20:31
  • @aynber Thanks! So if I want to change more than one role, how would you suggest I do that? I know I could just execute the add_cap function repeatedly, but I'd prefer to make this as dry as possible, so having one function that can have various roles added to it in order to update them as needed would be ideal. – scareaphina Feb 17 '23 at 21:00
  • Thanks @disinfor! I am trying to add capabilities to those two roles. – scareaphina Feb 17 '23 at 21:01
  • @scareaphina I see. The original question was about the error, which we answered - that's why it was marked as a duplicate. If you have another question that's unrelated to the error, you should close/delete this one (since you have your answer) and ask another. Currently, the title no longer reflects what you want to do. Or, at least edit this question with a new title, and remove the code in the question that's causing the error. – disinfor Feb 17 '23 at 21:14

1 Answers1

0

First, create an array of role names and then use array_fill_keys() to create an associative array with these role names as keys and null as values. You then loop through this array and retrieve the corresponding WP_Role object using get_role() and then add the capability to that role.

So you can use an array of role names instead of an array of WP_Role objects, like this

public static function add_csv_capability():void {
    $rolesToPopulate = array('administrator', 'editor');
    $roles = array_fill_keys($rolesToPopulate, NULL);
    
    foreach ($roles as $role) {
        if ($role) {
            $role_obj = get_role($role);
            $role_obj->add_cap('use_csv_uploader');
        }
    }
}

I hope this helps you fix the issue you are facing.

AkrAm Khan
  • 99
  • 5