I have created a number of custom post statuses using the EdiFlow plugin. Now, what I am trying to do is, if the contribter visits the post edit page and the post status is "assigned" to automatically change into "work in progress". This would allow me to know and send them an e-mail when they start working on it (on post edit page load).
I am trying to use this code which doesn't seem to do anything
function educadme_assigned_to_inprogress( $post_id ) {
if ( current_user_can('edit_post', $post_id) && !current_user_can( 'manage_options' ) ) {
$post = get_post($post_id);
if (get_post_type()=='post' && $post->post_status =='assigned' && strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php')) {
$post->post_status = 'in-progress'; // use any post status
wp_update_post($post);
}
}
}
add_action('add_meta_boxes', 'educadme_assigned_to_inprogress');
Another simplified version with less conditionals might be:
function educadme_assigned_to_inprogress( $post_id ) {
if ( current_user_can('edit_post', $post_id) && !current_user_can( 'manage_options' ) ) {
$post = get_post($post_id);
$screen = get_current_screen();
if ($screen->base != 'post')
return;
if ($post->post_status =='assigned') {
$post->post_status = 'in-progress'; // use any post status
wp_update_post($post);
}
}
}
add_action('add_meta_boxes', 'educadme_assigned_to_inprogress');
Another similar version which kind of worked but caused an issue explained below was:
function educadme_assigned_to_inprogress( $post_id ) {
if ( current_user_can('edit_post', $post_id) && !current_user_can( 'manage_options' ) ) {
$post = get_post($post_id);
if (get_post_type()=='post' && $post->post_status =='assigned' && strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php')) {
$post->post_status = 'in-progress'; // use any post status
wp_update_post($post);
}
}
}
add_action('add_meta_boxes', 'educadme_assigned_to_inprogress');
The main issue with this code is that it runs not just when the custom status is assigned. Also, it runs on the page listing all the posts, not just inside the individual edit post page. Meaning, if I am viewing the list of posts, it changes their post status!
Any advice or corrections would be appreciated. I am sure others would also find this of use.