2

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.

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

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.

4
  • I cannot seem to figure out where to add the code. Everywhere I add it I get Fatal error: Call to undefined function wp_get_current_user() in /home1/******/public_html/********/wp-includes/user.php on line 227. I will definitely take your advice about the options page, but I figure I should take it one step at a time. I would be happy to hear your recommendations for an options page as well. Apr 20, 2012 at 21:57
  • Can you post the code you used to add the metabox? The action and the function that actually creates it. If I can see it, I can better understand where it would be best implemented and hopefully figure out why it isn't working for you. Feel free to message it to me if you don't want to post it for some reason.
    – Eric Allen
    Apr 21, 2012 at 20:25
  • Nevermind, I see there's a pastebin of it. I'll take a look.
    – Eric Allen
    Apr 21, 2012 at 20:26
  • Did you try putting it around the add_meta_box() on line 20? I also may have thought about this incorrectly, try replacing the $user_id = line and the $user = line with $user = wp_get_current_user(); and let me know if that works. Sorry, I'm not somewhere that I can try to run the code so you'll have to bear with me.
    – Eric Allen
    Apr 22, 2012 at 3:40
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.