Restrict allowed file types for BuddyPress Avatar and Cover Images
BuddyPress allows your users to use "jpeg", "gif", "png" file types for avatars and cover images. If you need to restrict or change the allowed file types, there exists a filter "bp_attachments_get_allowed_types" that allows us to do it.
Let us see some examples on how to restrict the allowed file types for avatars as well as the cover image on a BuddyPress site.
Restricting allowed file types for BuddyPress User & Group Avatars
You can put the following code in your bp-custom.php or in your theme/child theme's functions.php to restrict the file type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Restrict allowed file types for BuddyPress User and group avatar. * * @param array $exts extensions. * @param string $type attachment type. * * @return array */ function buddydev_filter_allowed_bp_avatar_file_types( $exts, $type ) { if ( 'avatar' !== $type ) { return $exts; } /** * Add a comma separated list of extensions eg. 'png', 'jpeg', */ $exts = array( 'jpeg' ); return $exts; } add_filter( 'bp_attachments_get_allowed_types', 'buddydev_filter_allowed_bp_avatar_file_types', 10, 2 ); |
Make sure to change the $exts array with your own allowed types.
Result:-
Restricting allowed file types for BuddyPress User & Group Cover Image
Again, we use the same filter but check for the cover_image based o n type parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Restrict allowed file types for BuddyPress User and Group Cover Image. * * @param array $exts extensions. * @param string $type attachment type. * * @return array */ function buddydev_filter_allowed_bp_cover_file_types( $exts, $type ) { if ( 'cover_image' !== $type ) { return $exts; } /** * Add a comma separated list of extensions eg. 'png', 'jpeg', */ $exts = array( 'jpeg' ); return $exts; } add_filter( 'bp_attachments_get_allowed_types', 'buddydev_filter_allowed_bp_cover_file_types', 10, 2 ); |
Result:-
It works same as avatar. You can put it in your bp-custom.php or theme's functions.php
Have fun.