I'd like to show the add media button and for it to give access to only the logged in user's gallery.
Similar to the CRED forms 'Upload or select image' button.
The image I've uploaded gives full access, which I am trying to avoid.
The reason I want to do this, is to allow my users to create a biography, whilst being able to format it with images and text, rather than just text.
We have option using which you can control its visibility from your form settings as you can see with the following screenshot:
=> hidden link
You can enable its Add Media for users with the right upload_files capability (by default, authors and above).
So by default it will not be displayed to guest users. It will be displayed with upload_files capability but there is no option to show/hide it based on user loggedin or not.
To control the "Add Media" button based on the loggedin user role, you should try to add the following code to your current theme's functions.php file:
function func_media_buttons_from_desc_fields(){
// $post not available with init hook
$postID = url_to_postid( $_SERVER['REQUEST_URI'] , '_wpg_def_keyword', true );
$target_pages = array(9999 ); // Edit for pages with CRED forms
if ( in_array( $postID, $target_pages ) ) {
global $current_user;
if(!in_array('administrator',$current_user->roles)) {
remove_action( 'media_buttons', 'media_buttons' );
}
}
}
add_action( 'init', 'func_media_buttons_from_desc_fields' );
Where:
- You should replace 9999 with the page ID where the form is added.
Currently the "Add Media" button should be displayed with administrator role.
I have the form part activated, but only shows to author and above.
I want to enable it for other User roles, but at the same time, not allowing them to view ALL media within the Media Library.
So, just to follow up on this Minsh, when I add this code, it will show for the users, however, they'll only have access to their own gallery, not the administrator media library?
As you can see with the following screenshot, for the author role I've unchecked the checkbox that means author can only see the media uploaded by author but it does not have effect with "Add Media" button.
=> hidden link
As a workaround, if you want to display images uploaded by only loggedin user, you can add the following code to your theme's functions.php file: