Quantcast
Channel: BIOSTALL » PHP
Viewing all articles
Browse latest Browse all 57

How To Allow Editors Access To a Single Plugin in WordPress

$
0
0

Sometimes the default permissions given to user roles in WordPress need to be tweaked to match the needs of a particular project. We come across this quite a lot whereby we need to hide or show various different options for different user roles.

This can normally be achieved by using a plugin, such Adminimize, or by having a quick search around and copying some code into your theme’s functions.php file.

I had a requirement on a recent project that required users in WordPress of type ‘Editor’ be given access to a single plugin. I set about to find the answer and finally managed to come up with the solution outlined below.

The Solution

For the solution below I’ll be basing it on one of my own plugins as an example. The solution should be similar for all plugins, although you will have to delve into the plugins code (we’ll come to this in a short while).

In summary, in a plugin there will normally be a line of code that adds the plugin’s settings to the left menu within WordPress under ‘Settings

wordpress-plugin-settings
Within the plugin’s code this may look like so:

if ( is_admin() ) 
{
    add_action( 'admin_menu', 'my_plugin_menu' );
}

function my_plugin_menu() 
{
    add_options_page('My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin-settings', 'my_plugin_admin_page');
}

The above is calling the add_options_page() function to add the menu item whilst specifying things like how it appears, and the callback executed when someone visits the page.

Now, the problem is that the above is adding a submenu link under the main ‘Plugins’ menu item. An Editor won’t have access to this ‘Plugins’ menu and therefore none of these submenu items either.

In my scenario I didn’t really want to give them access to the whole plugins section so would move it to be in the main menu for Editors only.

If you didn’t develop the plugin in question…
More times than not, you probably didn’t develop the plugin in question. In this scenario you need to open up the plugins code and search for the phrase ‘add_options_page‘. This should hopefully present you with the code that we will use for the next step

Once we know how the call to ‘add_options_page‘ looks, we can begin the next step and add some code to our theme’s functions.php file. First, let’s remind ourselves of what the ‘add_options_page‘ looks like in the plugin we’re using as an example:

add_options_page('My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin-settings', 'my_plugin_admin_page');

And now, using the above, let’s look at what we should add to our functions.php file to allow access to the plugin for Editors

if ( is_admin() )
{
    add_action( 'admin_menu', 'add_plugin_for_editors' );
}

function add_plugin_for_editors() 
{
    if (!current_user_can( 'manage_options' ))
    {
    	add_menu_page('My Plugin Settings', 'My Plugin', 'moderate_comments', 'my-plugin-settings', 'my_plugin_admin_page');
    }
}

As you can see, we’re using the same parameters, but instead using the add_menu_page() to add a menu item at the top level. Now, if you login as an Editor you should see the normal options that Editors get, but also a new menu item containing a link to the single plugin.


Viewing all articles
Browse latest Browse all 57

Trending Articles