Tagged: code, mediapress-snippet
I am using mediapress to create galleries, the problem is that I would like the option to be available only for the administrator role.
- This topic was modified 6 years, 1 month ago by
Brajesh Singh.
- This topic was modified 6 years, 1 month ago by
Hello Antonio,
Thank you for posting. Please use the following code disable gallery tab for non-admin users.
/** * Modify gallery tab permission * * @param bool $is_active Can create gallery or not. * @param string $component Component name. * @param int $component_id Component id. * * @return bool */ function buddydev_modify_gallery_tab_permission( $is_active, $component, $component_id ) { if ( 'members' == $component && ! is_super_admin( $component_id ) ) { return false; } return $is_active; } add_filter( 'mpp_is_enabled', 'buddydev_modify_gallery_tab_permission', 10, 3 );
Thank You
Ravithanks excellent contribution and if I wanted a specific role besides the admin was able to create galleries
Hi Antonio,
In that case, we will need to first create a function to test for role like this/** * Check if a user has given WordPress role. * * @param int $user_id user. * @param string $role role. * * @return bool */ function buddydev_user_has_role( $user_id, $role ) { $user = get_user_by( 'id', $user_id ); if ( empty( $user ) ) { return false; } $user_roles = $user->roles; if ( empty( $user_roles ) ) { return false; } return in_array( $role, $user_roles, true ); }
And then, we can use it like below(I am only enabling for administrator in this case)
/** * Only allow galleries for a certain role. * * @param bool $is_active Can create gallery or not. * @param string $component Component name. * @param int $component_id Component id. * * @return bool */ function buddydev_enable_gallery_for_role_only( $is_active, $component, $component_id ) { if ( 'members' == $component ) { $is_active = buddydev_user_has_role( $component_id, 'administrator' ); // 'editor', 'contributor', 'subscriber' } return $is_active; } add_filter( 'mpp_is_enabled', 'buddydev_enable_gallery_for_role_only', 10, 3 );
That will do it for you.
Regards
BrajeshHi Antonio,
Hope you are doing well.Can you please tell me if the above worked for you or not?
Thank you
Brajesh
You must be logged in to reply to this topic.