1

I have created a custom post type in a plugin for a site I'm working on. I'm allowing users assigned a custom role to add/edit/delete the custom post type. I would like to give these users as little functionality as possible in the admin beyond posting.

So I've used remove_meta_box to remove a few panels on the conversation post edit screen for these users via a plugin. One of the meta boxes I've disabled is commentsstatusdiv which includes the form elements for comment status and trackbacks.

It seems that if the comments status form element is missing, the comment_status field gets set to off. I'd like to default comments on for the post type and prevent users in this role from changing the comments setting. But when the users save the post comment status is set to off.

I can force a setting with the wp_insert_post_data filter. But I don't want to override for admins. I'd like admins to have control.

How do I force comments status to be on by default and prevent users of a specific role from changing it while still allowing admins to turn them on/off?

3
  • you could hide it with CSS. not bulletproof obviously.
    – Milo
    Commented Jun 8, 2011 at 19:47
  • 2
    Stick with your filter, but check current_user_can( 'limited_role_name' ) before you enforce the comment status. Commented Jun 8, 2011 at 20:00
  • The trouble though is that there's no way to retain the comment_status on edits by non-admin users. With the comment div removed on edit save it reverts back to "off." Commented Jun 9, 2011 at 1:18

1 Answer 1

2

Here's what I ended up with. For limited access users, I set comments on when the post guid is empty. Otherwise, I completely remove the comment_status field for those users. That defaults new posts to comments enabled, prevents limited access user edits from switching them off, while allowing admins to override the setting on/off.

add_filter( 'wp_insert_post_data', 'handle_comments_setting' );
function handle_comments_setting( $data ) {
  if ( current_user_can( 'limited_role_name' )) {
    if ( $data['guid'] == '') {
      //Default new posts to allow comments
      $data['comment_status'] = "open";        
    } else {
      //Otherwise ignore comment setting for community_member role users
      unset($data['comment_status']);
    }
  }
  return $data;
}

Your Answer

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

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