Madhu
Madhu

Reputation: 1

wordpress submit for review instead of update on page for author user?

I'd like to get "submit for review" for author user when they want to updating or changing into a page. This is my code which works only after clicking on update button then it will show "Submit for review". I don't want to update button on page when author user is working on this. Thanks.

function postPending($post_ID) { 

$c_user = wp_get_current_user();

    if( $c_user->user_login == "user1")

   //if(get_role('author')) {

      remove_action('post_updated', 'postPending', 10, 3);

     return wp_update_post(array('ID' => $post_ID, 'post_status' => 'pending review' ));

     add_action( 'post_updated', 'postPending', 10, 3 );
   }
 }

 add_action('post_updated', 'postPending', 10, 3);

Upvotes: 0

Views: 731

Answers (1)

Tejas Gajjar
Tejas Gajjar

Reputation: 248

You can use below code and put this into function.php.

add_action( 'init', function() {
    $author_role = get_role( 'author' );
   $author_role->add_cap('publish_posts');
   $author_role->add_cap('edit_posts',true);
} );
function postPending($post_ID,$post_after,$post_before)
 {  
    $display_name = get_the_modified_author();
    global $wpdb;

    $user = $wpdb->get_row( $wpdb->prepare(
        "SELECT `ID` FROM $wpdb->users WHERE `display_name` = %s", $display_name
    ) );
    $user_meta = get_userdata($user->ID);
    $user_roles=$user_meta->roles;

     if(in_array('author', $user_roles))
     {
        //Unhook this function
        remove_action('post_updated', 'postPending', 10, 3);

        return wp_update_post(array('ID' => $post_ID, 'post_status' => 'pending'));

        // re-hook this function
        add_action( 'post_updated', 'postPending', 10, 3 );
     }
 }
add_action('post_updated', 'postPending', 10, 3);

Do let me know the result.

Upvotes: 1

Related Questions