Support » Fixing WordPress » assign images to user-level / restrictions for media library items?

  • Hello, i am looking for a way to individual restrict certain images in my media library for a certain level of registered user. lets say i have a post with a gallery including 50 photos, but only the admin or a registered client should be able to see all photos in that post. An unregistered user should only see a certain “public” selection of photos. All plugins i have found so far are regulating the rights for the whole post, but i would like to only assign images to certain user rights / level, not the whole post. Any help would be appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Mikono,

    Depending on how you are inserting your gallery or galleries in your post(s), what I would do is have two different versions of your galleries: one for admins and registered users, and one for non-registered users and visitors. Then, you can use a php if statement to check whether the user is logged in: if the user is logged in (ie., is a registered user), the full version is displayed; if he is not logged in, the limited version is displayed.

    To do this, first you need to download and install these three plugins:
    1. Exec-php,
    2. Role Manager, and
    3. Multiple Galleries.
    Exec-php allows you to run php code in your posts, pages, and widgets. You need to follow the plugin’s instructions carefully so as to not let just any user do this.
    Role Manager allows you to manage WP user capabilities and gives you more control over user permissions. It is required by Exec-php to control which user levels are allowed to write and execute PHP code.
    Multiple Galleries inserts a checkbox next to each image in the Gallery section, making the creation of multiple galleries way easier.

    Next, edit/create a post or page in which you wish to display multiple galleries, and paste this code into that post or page, where you want the gallery to appear:

    <?php if ( is_user_logged_in () ) { ?>
    <?php } else { ?>
    <?php } ?>

    The short code [gallery] will display the entire gallery, but only if the user is logged in. If the user is not logged in, the else segment of the condition will be executed. What you need to do to make it work, is replace [insert_visitor_gallery_here] with the short code for the limited version of the gallery. To do this, simply highlight that text, click on the ‘Add object’ icon, go to Gallery, check only the images that you want visitors to see, and click on Insert Gallery button.

    And presto, you are done! You can repeat the process for each post in which you wish to display multiple galleries.
    I hope this works for you.

    Good luck!

    Hi Marventus,
    thanks a lot for your message. I have “automated” the insertion of the gallery, because i only post photos / photo albums, so i actually don’t use the textfield at all ( i might use custom fields instead). I already thought about the two gallery solution and this is what i am actually doing right now in a little different way. I use the plugin “User Access Manager” and you can create User groups, so i basically create for each user a new user group and restrict the post only for this group. this works fine so far, but it is too much work on a long term. the solution you have mentioned would makes a gallery visible to all subscribers / logged in users. i was more hoping to find a solution where i can connect / assign specific images of a gallery to one (or more) specific user.
    So i was hoping that there is a plugin out there that similar to “multiple galleries” has a little check box or even better assign images to a user / user group as “User Access Manager” does it with posts. i am surprised that there seems nothing out there..

    Anyway, thanks so much, i will definitely keep this in mind, because it is actually interesting for an other project i might will use your trick! 🙂

    i’ll keep you informed if i find a solution..

    Hi Mikono,

    I’m sorry that is not exactly what you were looking for. I tried looking for plugins that would let you assign user levels to images but couldn’t find any.
    That’s when I came up with the above solution.

    When you say you would like to be able to

    connect / assign specific images of a gallery to one (or more) specific user

    do you mean individual users by user ID or username, or an user role (such as “administrator”, “editor”, author, etc.)?
    I’m asking because the above solution can be tweaked to check for the user role or level instead of for whether the user is logged in or not.

    Another thing I thought of, but couldn’t actually carry to term because I’m not a programmer, is to use the images’ Description fields. For instance, if a picture is to be seen by administrators only, or by authors only, you could write ‘admin_photo’ or ‘author_photo’ respectively in its description, and then only call pictures that have ‘admin’ or ‘author’ inside their description.

    The following code:

    $args = array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => null,
    	'post_status' => null,
    	'post_parent' => $post->ID
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, false);

    retrieves all images attached to the current post. What I couldn’t figure out is how to filter them according to their description.

    I just wrote smth here but it was in the wrong thread.
    Sorry for that!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘assign images to user-level / restrictions for media library items?’ is closed to new replies.