Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
you could hide it with CSS. not bulletproof obviously. –  Milo Jun 8 '11 at 19:47
2  
Stick with your filter, but check current_user_can( 'limited_role_name' ) before you enforce the comment status. –  TheDeadMedic Jun 8 '11 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." –  Dave Konopka Jun 9 '11 at 1:18
add comment

1 Answer

up vote 1 down vote accepted

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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