Enable comments for post with comments meta box removed

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.

Read More

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?

Related posts

Leave a Reply

1 comment

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