Support » Plugin: User Role Editor » Allowing user to edit posts based on the post status

  • I have Edit Flow and User Role Editor installed on my news site and I basically wanted to do a system where editors pitch ides worth writing about to contributors. So I would want my contributors to be able to only edit posts where post status is set to pitch. The idea is: they login, see a couple of pitched posts, pick the one they like, set the status to assigned and themselves as author and from that moment only they and editors can edit the post. Currently I have it setup so all contributors can edit all unpublished posts so I would appreciate your help.

Viewing 1 replies (of 1 total)
  • Hi @nnitramm – I do something very similar…..in our case we do not use the role of “Author” for any user, they are Contributors, or Editors/Admins. We did this because the default behavior of WP is to allow Authors to Publish, but Contributors can only set posts to Pending Review.

    SO we list ‘open’ (unassigned) posts on the front end and use a form (GravityForms) to allow our authorized and logged in contributors to ‘claim’ a topic……I have a form modification script snippet that then changes the status of the post from ‘open’ to ‘pitched’ and the author from a user called ‘unassigned’ to the contributor that submitted the form, and Edit Flow notifies me of the change.

    I then approved it by manually modifying the post from ‘pitched’ to assigned and it shows up in the list of posts the user can then edit in order to submit their post. Although our system doesn’t force them to, we ask them to change the status to ‘in progress’ as they are working on it so our editors are aware, and then to ‘pending review’ when they feel it’s ready for an editor to look at it. Only editors can change it then to scheduled or published.

    So the whole process is:
    1. Editors decide on post ‘topics’ (titles) and the default status is ‘Open’ with the default user of “Unassigned”.

    2. Authors peruse the list of Open posts and fill in a form to claim one that they like (we also ask them to limit their ‘claims’ to 2 at any one time, so they don’t hog all the posts. 🙂

    3. The form submission changes the status to ‘Pitched” and the Author to the user who submitted the form.

    4. All editors are notified by Edit Flow, and any editor can then approve the pitch by manually changing the status to “Assigned”.

    From that point the contributor is given a time limit to complete the post (editors just keep track)…if the post is submitted in the time frame – great, it gets edited and published, if not it gets returned to Open status and the contributor is given a warning. After 3 warnings they are no longer allowed to pitch.

    This system requires us to make these modifications, most via our functions.php file:

    1. Change the ‘default’ post status when creating a new post from ‘draft’ to ‘open’ – this is not a script mod, it’s done via Edit Flow settings

    2. Block all contributors and authors from seeing posts in the back end that have not been assigned to them – they can view and claim them from the front end. Once an editor approves and assigns, they can then see them in the back end

    // Blocks users (authors & contributors) from seeing any posts except their own
    add_action( 'load-edit.php', 'posts_for_current_contributor' );
    function posts_for_current_contributor() {
     global $user_ID;
    		if (!current_user_can( 'delete_others_posts' ) ) {
           if ( ! isset( $_GET['author'] ) ) {
              wp_redirect( add_query_arg( 'author', $user_ID ) );
              exit;
           }
       }
    }

    We also had to block contributors from certain statuses using this

    // Mods to Edit Flow Plugin
    function efx_limit_custom_statuses_by_role( $custom_statuses ) {
        $current_user = wp_get_current_user();
        switch( $current_user->roles[0] ) {
            // Only allow a contributor to access specific statuses from the dropdown
            case 'contributor':
                $permitted_statuses = array(
                        'assigned',
                        'in-progress',
                        'pending',
                    );
                // Remove the custom status if it's not whitelisted
                foreach( $custom_statuses as $key => $custom_status ) {
                    if ( !in_array( $custom_status->slug, $permitted_statuses ) )
                        unset( $custom_statuses[$key] );
                }
                break;
        }
        return $custom_statuses;
    }
    add_filter( 'ef_custom_status_list', 'efx_limit_custom_statuses_by_role' );
    

    AND in order to allow Contributors to upload photos which the standard WP role of Contributor normally can’t, we added this:

    // Allows contributors to upload media (images) 
    add_action('admin_init', 'allow_contributor_uploads');
    function allow_contributor_uploads() {
    	$contributor = get_role('contributor');
    	$contributor->add_cap('upload_files');
    }
    

    Alternatively you could just prevent Authors from publishing posts, as Authors can upload photos – we just decided to go this route.

    3. Create a GravityForm that loops through all “Open” posts and displays them in a dropdown list (I also display them on the page above the form with info taken from the post that is entered into the EditFlow metadata fields, such as how much the post will pay and what it should cover – that’s a fairly standard WP loop code but I can post it if you need it, here I’ll just post the GravityForm modification code)

    add_filter( 'gform_pre_render_6', 'dynamic_populate_topicsList' );
    add_filter( 'gform_pre_validation_6', 'dynamic_populate_topicsList' );
    add_filter( 'gform_pre_submission_filter_6', 'dynamic_populate_topicsList' );
    add_filter( 'gform_admin_pre_render_6', 'dynamic_populate_topicsList' );
    
    function dynamic_populate_topicsList($form) {
     
     foreach ( $form['fields'] as &$field ) {
         if ( $field->type != 'select' || strpos( $field->cssClass, 'topicsList_dropdown' ) === false ) {
         continue;
       }
     $args = array(
            'post_type'       => 'post',
    		'post_status'     => 'open',
            'orderby'         => 'title',
            'order'           => 'ASC',
            'numberposts'     => -1
         );
     $posts = get_posts($args);
     $choices = array();
     foreach ( $posts as $post ) {
        $choices[] = array( 'text' => $post->post_title, 'value' => $post->ID );
      }
      $field->placeholder = 'Please Select';
      $field->choices = $choices;
      }
      return $form;
    }
    

    Note that in the ‘add filter’ section at the top ‘6’ refers to the ID of our Gravity Form

    Then we add a second mod to the form so that on form submission the status and author are changed:

    // Changes the Author to current user and the status to 'pitched' when using Topics Submission form 
    add_action( 'gform_after_submission_6', 'set_post_status', 10, 2 );
    function set_post_status( $entry, $form ) {
    	// getting post ID from field 1
    	$postID = rgar ( $entry, '1' );
    	$current_user = wp_get_current_user();
    	$the_author_id = $current_user->ID;
    	// changing post status
    	if ( $postID ) {
    		// updating post
    		wp_update_post( array( 'ID' => $postID, 'post_status' => 'pitched', 'post_author' => $the_author_id  ) );
    	}
    }
    

    Lastly, we also had to do a little cleanup to remove some menu items such as the “Add New” menu item under Posts and the “Add New” button, and I needed to remove a bunch of metaboxes from the Post Edit screen that were added by other plugins that we didn’t want contributors messing with, but this system works pretty well for us.

    I hope this helps! Post back if you have any questions on what we’re doing, or need suggestions on how to tweak this to work for you. Good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Allowing user to edit posts based on the post status’ is closed to new replies.