Skip Navigation

[Resolved] Show 'Add Media' button without administration permissions

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by SteBlood 4 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1658147
Annotation 2020-02-26 102700.png

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.

Is this possible?

#1658481

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support

Are you talking about t"Add New" button available at Media => Media Library? If no, can you please share what media button you want to control?

#1658499
Annotation 2020-02-26 102700.png

Hi Minesh, please take a look at the screenshot.

#1658599

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - thanks.

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.

#1658693

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?

#1659639

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There seems an issue as when I set the Media permission for the form using Access control for the forms:
=> https://toolset.com/documentation/user-guides/access-control/access-control-for-cred-forms/

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:

add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );
function wpb_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
        $query['author'] = $user_id;
    }
    return $query;
} 
#1661289

My issue is resolved now. Thank you!