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.

Read More

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.

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

Related posts

Leave a Reply

2 comments

  1. 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.

  2. Thanks to ericissocial for sending me in the right direction.

    I decided to go with:

    if(current_user_can( 'edit_others_posts' )) {
        //add meta box
    }
    

    This way any custom user role with edit_others_posts can use the function.