TransWikia.com

Wordpress user permission to allow non admin user to access plugin content

Stack Overflow Asked by Wis on December 11, 2021

I need to give access to a non admin user to the content of a wordpress plugin (WP booking system), I’d like the user to be able to access the plugin content but without giving him full admin access.

I’ve tried some user permission management plugin but i can’t find the correct permission to give to the role. I think it’s possible as with the ‘editor’ role, i can have a user that access the ‘contact form 7’ plugin

Do you have a tools for it or maybe i need to edit the plugin ?

Thanks a lot for your help

One Answer

WP Booking System, in case you're using the latest version (2.0.11, can't tell for older versions), supports a filter for the capability name required to access the plugin's admin page. You can just add a filter to modify the required capability name to access this specific admin page:

add_filter('wpbs_menu_page_capability', 'change_wpbs_menu_capability', 10, 1);
function change_wpbs_menu_capability( $cap ) {
  $capability_name = 'my_wpbs_capability_name'; // <- change this name to whatever you like
  return $capability_name;
}

Now, the my_wpbs_capability_name capability is required to view the Booking System admin page. You can name this capability whatever you like.

You now use another WP plugin for user roles/capabilites editing (e.g. User Role Editor, PublishPress Capabilities) and create a new user role (or modify an existing one) with limited access (no plugin editing, limited post editing) and grant this particular user role the my_wpbs_capability_name capability (or whatever you named it). Next, create a test user with this specific role, log into wp-admin in an incognito tab and check whether the user still has too much access. Use the role editor plugin to modify the role's access to match your requirements. The user should still be able to see the WP Booking System admin page.

Edit: Regarding your question with the Google docs screenshot:

  1. You named your capability "wilfried". While this is of course alright, I just want to make sure I didn't confuse anyone by saying "change this name to whatever..". It still is a capability name given to a user role, not a WordPress user or role name.
  2. You had an error in your code complaining about add_filter. This is because I think you tried using a class method. If it indeed is inside a class, you have to do it differently:
class MyPluginClass {
  // ...

  // make this function static:
  public static function change_wpbs_menu_capability( $cap ) {
    $capability_name = 'my_wpbs_capability_name'; // <- change this name to whatever you like
    return $capability_name;
  }

  // this also has to be static:
  public static function hooks() {
    // the function reference must be an array of class, method name:
    add_filter('wpbs_menu_page_capability', [self::class, 'change_wpbs_menu_capability'], 10, 1);    
  }
}

// call the static hooks method to register your filter:
MyPluginClass::hooks();

Make very sure to call the hooks function (or whatever you name it) after the class definition. Making these methods static is not the one and only way, but in this case it's more consistent. In case you're ever creating more than one instance of MyPluginClass, just one filter method needs to be registered (as opposed to adding a filter for every instance - which in this case is useless).

Answered by Damocles on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP