Plugin Functionality Only for Editor and Administrator

I’ve never actually written a WordPress plugin before, so this is my first crack at it.

Basically, the plugin is for multi-author blogs where post authors must submit their post for review. The Editor/Administrator must come in and approve and publish/schedule the post. All of this is good, and built into WordPress.

This plugin adds the ability for the Editor/Administrator to leave a comment on the post content, not suggestions for improving the post. This is particularly useful when the Editor schedules the post to publish at a future date/time.

The Editor can leave their comment, then when the post publishes, the editor’s comment automatically publishes with it.

  • I have created a custom meta box with a textarea following this
    method – Add A Meta Box In WordPress
  • I have set the form to save to the database and publish when the post
    is published using – How can I allow editors to leave comments on
    posts that have not yet been published?

The final thing I cannot figure out is how to make this plugin active only for editor and administrator roles. I do not want the post author to be able to leave comments before the post is published.

Essentially, I don’t want to require the post editor, who has already read the post, to come back later to leave his/her comments.

Edit

Pastebin of complete code – http://pastebin.com/yG9uqJ7q

2 Answers
2

You could check the user role when loading your metabox.

//get user
$user_id = get_current_user_id();
$user = new WP_User($user_id);
//check user role
if($user->roles[0] == 'administrator' || $user->roles[0] == 'editor') {
     //add your metabox here
}

That way the metabox is only added to the post for editor’s and users, you’ll probably want to wrap your save functionality in a similar condition so that it doesn’t try to run every time the post is saved.

EDIT: If you plan to use this on any other site or release it for others to use, I’d also recommend setting up an options page for your plug-in so that the user roles that are able to use this plug-in can be selected from a list of current user roles.

That way custom user roles can be accounted for, as well.

Leave a Comment