User Roles - add role capabilities php snippet - Wordpress

Viewed 237

I had User Role Editor plugin installed and yes I pressed the button that said 'Reset Roles'. Silly mistake, but since I've restored all user roles but for some strange reason that I'm yet to figure out, I've been loosing capabilities for one of my roles. They seem to just disappear randomly and I'm not sure why is it happening.

But to fix this, I've placed the following snippet into my functions.php file:

function restore_librarian_permissions() {
  $librarian = get_role('librarian');

  $librarian->add_cap('upload_files');
  $librarian->add_cap('edit_books');
  $librarian->add_cap('publish_books');
  $librarian->add_cap('edit_published_books');
  $librarian->add_cap('delete_books');
  $librarian->add_cap('delete_published_books');
}
add_action('admin_init', 'restore_librarian_permissions');

It works fine, but my question is, "Is it okay to leave this snippet as is live and active in functions.php, so it keeps executing?".

Or could this be possibly causing any errors?

1 Answers

I think it's better to run it only once, because every time you load any page as the database tables will keep getting overwritten on every page load.

Related