Use the following code to limit 10 upload for specific role
add_filter( 'wp_handle_upload_prefilter', 'limit_uploads_for_user_roles' );
function limit_uploads_for_user_roles( $file ) {
$user = wp_get_current_user();
// add the role you want to limit in the array
$limit_roles = array('contributor');
$filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles, $user );
if ( array_intersect( $limit_roles, $user->roles ) ) {
$upload_count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
$limit = apply_filters( 'limit_uploads_for_user_roles_limit', 10, $user, $upload_count, $file );
if ( ( $upload_count + 1 ) > $limit ) {
$file['error'] = __('Upload limit has been reached for this account!', 'yourtxtdomain');
} else {
update_user_meta( $user->ID, 'upload_count', $upload_count + 1 );
}
}
return $file;
}
This action will fire when user delete attachment
add_action('delete_attachment', 'decrease_limit_uploads_for_user');
function decrease_limit_uploads_for_user( $id ) {
$user = wp_get_current_user();
// add the role you want to limit in the array
$limit_roles = array('contributor');
$filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles, $user );
if ( array_intersect( $limit_roles, $user->roles ) ) {
$post = get_post( $id);
if ( $post->post_author != $user->ID ) return;
$count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
if ( $count ) update_user_meta( $user->ID, 'upload_count', $count - 1 );
}
}
Thanks Giuseppe
Need Help with Your Digital Presence?
At Dynamic Web Lab, we specialize in building fast, scalable websites and applications for businesses across the UAE and beyond.
- View our case studies to see recent projects
- Explore our services — from WordPress to Next.js, AI solutions to mobile apps
- Contact us for a free consultation
Whether you need a WordPress website, a custom Next.js application, or AI-powered solutions, our team is ready to help.
Written by
Dynamic Web Lab Editorial
We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.
Need help implementing this?
We turn these playbooks into shipped features. Let us scope your roadmap and support your team.
Start a project conversation