For users with the Contributor
user role, I want to restrict the total amount of images that they are allowed to upload. I found this code posted on an answer here:
add_filter( 'wp_handle_upload', 'wpse47580_update_upload_stats' );
function wpse47580_update_upload_stats( $args ) {
$user_id = get_current_user_id();
$upload_count = get_user_meta( $user_id, 'upload_count', $single = true );
update_user_meta( $user_id, 'upload_count', $upload_count + 1 );
}
add_filter( 'wp_handle_upload_prefilter', 'wpse47580_check_upload_limits' );
function wpse47580_check_upload_limits( $file ) {
$user_id = get_current_user_id();
$upload_count = get_user_meta( $user_id, 'upload_count', $single = true );
$upload_count_limit_reached = apply_filters( 'wpse47580_upload_count_limit_reached', 10 ) > ( $upload_count + 1 );
if ( $upload_count_limit_reached )
$file['error'] = 'Upload limit has been reached for this account!';
return $file;
}
There are two problems. Firstly, the above code does not function properly, it is supposed to allow 10 uploads but the upload limit message is displayed no matter what. Secondly, it applies to all user roles.
Upon further research, I found this working answer, but the solution applies for total uploads per post, not user.
How can I set an upload count limit for users on a specific user role? Ideally with a solution that deducts the count if user permanently removes an uploaded image.