How to Extend Members with Functions

Posted OnJan 9, 2015     CategoryQuestion & Answer     CommentsNo comment

In this manual, we will show you how to extend the Members plugin through writing some basic PHP functions.

Checking if the current user has a capability/role

In plugins and your theme template files, you may need to check if the currently logged in user has permission to do something.

This can be accomplished by using the WordPress function current_user_can().

The basic format looks like this:

km-members-php-01a

For a more practical situation, let’s say you created a new capability called read_pages.

You may want to hide the content within your page.php template by adding this:

km-members-php-02a

Only users with a role that has the read_pages capability will be able to see the content.

You can check for a specific role by inputting the role name instead of the capability name.
It works the same way.

Adding new default capabilities

Your plugin/theme can add new capabilities to the Edit Roles component if needed. This will allow users to easily select the additional capabilities for whichever roles they choose.

km-members-php-03a

You need to respect the existing capabilities and return the original array.

Checking for capabilities

In WordPress, you can use the current_user_can() function to check if the current user has a particular capability. Since you don’t know whether a user has this plugin installed, you might want to check first.

The members_check_for_cap() function (only use in admin) checks if any role has a particular capability. This can be useful in setting up something like admin menus.

For example, you can set up a theme settings menu for users that have the edit_themes capability. But, if this plugin is installed and a user has the edit_my_theme capability, that’ll be used instead.

km-members-php-04a

Old User Levels

Some plugins and themes might rely on the old user level system in WordPress. These were deprecated in WordPress version 2.1 and should not be used at all. WordPress still has minimal legacy support for these, we suggest contacting your theme/plugin author if user levels are being used.

By default, the levels aren’t shown. They still exist, but are tucked away behind the scenes. While not recommended, if you need to control who has what level (levels are just capabilities), add this to your theme’s functions.php:

km-members-php-05a

This concludes our guide of the Members plugin.

 

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Send this to friend