Im currently using User Role editor to modify permission for the user role. I'd like to be able to restrict the role from publishing a post. They can save the draft but they should not be able to publish. Is there a better way to do it?
-
Not sure why you ask this, maybe I am wrong but that's just an option in User Role Editor. Select the role and add it to the role or just leave it inactive imho.– CharlesFeb 21, 2020 at 19:33
2 Answers
Use wp_insert_post_data
filter. Then check the user role. If user have specific role set-up post status. ( I don't test below script)
add_filter( 'wp_insert_post_data', 'filter_handler', '99');
function filter_handler( $data ) {
$user = wp_get_current_user();
$userRole = $user->roles ? $user->roles[0] : false;
if ( $userRole === 'author') {
$data['post_status'] = 'pending';
}
return $data;
}
-
-
1
functions.php
, 99 it's a priorityUsed to specify the order in which the functions associated with a particular action are executed.
more about add_filter and priority: developer.wordpress.org/reference/functions/add_filter/… Feb 21, 2020 at 20:12 -
it doesnt work unfortunately. The user can still publish the post. Feb 21, 2020 at 20:23
-
Okay, I did a mistake. Script was updated. Just change
author
role to yours. Feb 21, 2020 at 20:30