NextGEN Gallery – Restrict gallery access to specific users

  • December 4, 2012

Anyone using WordPress will most likely be aware of the NextGEN Gallery plugin as the most popular and widely used plugin for managing photo and image galleries. I’ve used it on a number of sites and love the way it is easily installed, managed and customised to provide unique and beautiful ways of displaying photos. The admin interface is also easy to use and allows for really handy features such as uploading photos in a ZIP file, automatic resizing of images to avoid long page load times, automatic thumbnail generation and even watermarking.

Recently however, a photographer client asked me if she could have client-only galleries on her site, where a client would login and view only their own photos prior to ordering their favourites. This seemed like a reasonable sort of feature to request, but I wasn’t able to find anything out there in the WordPress world to achieve it. The User Access Manager plugin looked perfect, but it hasn’t been updated in over 2 years and is no longer compatible with current versions of NextGEN Gallery.

After looking around for a while and evaluating a few options, I was able to come up with a neat solution that works well for my client and I thought I would post it in case it may help others also.

Step 1:

Install the NextGEN Gallery Custom Fields plugin. This plugin, like it says, allows you to create custom fields for your NGG galleries and images. For our purposes we only need to add one new field to each gallery, to allow an admin user to specify which user the gallery should be restricted to.

After installing, click on the NGG Custom Fields link in the menu and choose the Gallery Custom Fields option. I gave my field a display name of ‘Restrict to user’ and made it a text field, showing on all galleries which are client-only. New galleries will include this field by default which is what we want.

Step 2:

Install the User Specific Content plugin. This plugin, like it says, allows you to restrict access to certain posts/pages/content according to a user’s role or username. The plugin would probably provide a solution for my photographer client by itself, if she created a separate page for each client-only gallery and added it to her menu each time. I preferred to create a single client access page, however, and only list the galleries there which the logged in user should see. This simplifies the menu and avoids the need for lots of extra pages.

Once installed, I configured the plugin to style the login box and adjust the ‘blocked’ message to suit my needs.

Step 3:
Create a page where your restricted galleries will display. Mine is called ‘Client Area’. In the page content, I want to tell NGG which album of galleries to display, so I use the shortcode:

[album id=2 template=client]

This album will contain all of the client galleries to which I want to restrict access. By specifying the template to use, we can take advantage of the easy customisability of NextGEN Gallery and create a custom template which will display only the right galleries within this album for the current user.

I also need to tell the USC plugin that this page is one containing restricted content, so underneath the content window add a custom field with a name of block and a value of true.

Step 4:

This is where the custom code comes in to link the two plugins together and allow clients who login to see their private galleries. You’ll need to create a template called album-client.php with the following content:

 Template Page for the album overview (extended)
 Restricts view of galleries only to admin users or users set via NextGEN Custom Field
Follow variables are useable :
$album : Contain information about the album
 $galleries : Contain all galleries inside this album
 $pagination : Contain the pagination content
You can check the content when you insert the tag <?php var_dump($variable) ?>
 If you would like to show the timestamp of the image ,you can use <?php echo $exif['created_timestamp'] ?>
 <?php if (!defined ('ABSPATH')) die ('No direct access allowed'); ?><?php if (!empty ($galleries)) : ?>
<div class="ngg-albumoverview">
 <!-- List of galleries -->
 <?php global $current_user; get_currentuserinfo(); ?>
 <?php foreach ($galleries as $gallery) : ?>
<?php $restrictUser = nggcf_get_gallery_field($gallery->gid, "Restrict to user");
 //echo "Current username = " . $current_user->user_login . ". Gallery is restricted to user = " . $restrictUser;
<?php if (current_user_can( 'add_users' ) || $restrictUser == $current_user->user_login) { ?>
 <div class="ngg-album">
 <div class="ngg-albumtitle"><a href="<?php echo $gallery->pagelink ?>"><?php echo $gallery->title ?></a></div>
 <div class="ngg-albumcontent">
 <div class="ngg-thumbnail">
 <a href="<?php echo $gallery->pagelink ?>"><img class="Thumb" alt="<?php echo $gallery->title ?>" src="<?php echo $gallery->previewurl ?>"/></a>
 <div class="ngg-description">
 <p><?php echo $gallery->galdesc ?></p>
 <?php if (0 == 1 && $gallery->counter > 0) : ?>
 <p><strong><?php echo $gallery->counter ?></strong> <?php _e('Photos', 'nggallery') ?></p>
 <?php endif; ?>
 <?php } ?>
<?php endforeach; ?>
<!-- Pagination -->
 <?php echo $pagination ?>
<?php endif; ?>

Line 27 is the important one – it gets the username of the current user and compares it to the value of the custom field we created in Step 1. If they match, or this is an admin user, the gallery will be displayed, otherwise not.

Copy this file to


or even better, create a folder called nggallery within your theme and copy it there to avoid being overwritten when upgrading the NextGEN Gallery plugin.

Step 5:

Now we can create our client specific galleries. Use NGG to create a new gallery (or use an existing one) and upload some photos into it. When you select Manage Gallery and click on the name of your client gallery the admin window will show a new section called Custom Columns, which contains your new custom field, which I called ‘Restrict to user’. In this field type the username of the client to which you want to restrict access, then go and create a Subscriber user with this username if there isn’t one already.

In the ‘Album’ NGG menu option, make sure this client gallery is contained in the album that you specified in step 3.


That’s it! If you log out and go to your new Client Area page, you should see a login box. Login as the user you created in step 5 and you should see the client gallery created for this user. Log in as someone else (not an admin user as they see all galleries) and this gallery will disappear.

Was this post useful, clear, or just confusing? Leave me a reply below and I’ll try to help out.

34 Comments on NextGEN Gallery – Restrict gallery access to specific users

  • Matt Dunn says:
    January 2, 2013 at 12:16 pm

    Great post and clever solution. Is it possible to restrict access to a Group instead of an individual user?


    • admin says:
      January 2, 2013 at 2:57 pm

      By a ‘group’, I assume you mean a role?

      If so then yes, certainly. If you enter the name of a role for your custom field in Step 5 (you’d probably want to name it something like ‘Restrict to role’ instead) you can then check for it in album-client.php using the current_user_can function like this (replace line 27):

      < ?php if (current_user_can( 'add_users' ) || current_user_can($restrictUser)) { ?>

      Hope that helps!


  • branden says:
    February 21, 2013 at 3:25 am

    . I am trying to do external links from images and want only certain images to show up based on user name. is this also possible for the images within a gallery


    • admin says:
      February 21, 2013 at 9:56 am

      It should be, although I haven’t tried it. I’d use the NGG Custom Fields plugin to create a new field for Images, give it a Display Name like ‘Show for user:’ and then put in it the name of the user you want it to show for. If you want multiple users to see this image you could separate them with commas.
      Then you’ll need to create a custom gallery template, and when looping through the photos you’ll need to create an if condition to check if the name of the current user matches your new field, and display it if so.


  • branden says:
    February 21, 2013 at 11:30 am

    Thank you. Do you know how to do an external link from the gallery preview, therefore not even opening the gallery, just linking to the url? This would solve the previos issue.


    • admin says:
      February 21, 2013 at 1:31 pm

      So you just want to display the NGG thumbnails for each gallery on an album page and have them link to another site?
      I’d probably do that by creating a custom gallery field called URL and putting the link you want to go to in there for each gallery. Then in your custom album template you’d have to make some changes when looping through the galleries to get this field and set it as the href for each of the thumbnails.


  • branden says:
    February 23, 2013 at 12:05 am

    also, how do you set up the login page. mine currently is a password protected page (Projects), which you can view, but I don’t like that way. A login page would be much preferred.


    • admin says:
      February 27, 2013 at 4:31 pm

      I used the WP-Members plugin for the login page.


  • David says:
    June 20, 2013 at 8:46 pm

    Great post, just what I was looking for, for a site I am building for a friend! I am barely conversant with php, css and html so please forgive my ignorance if my questions seem simple to you.
    Is it possible to condense the images on the page rather than having them so spread out? Also I don’t know if I have missed something but I can’t find anyway of allowing the logged in user to edit their gallery.
    I may simply have a conflict with settings. I am using Weaver ii pro with S2 Members
    Kind regards


    • admin says:
      June 21, 2013 at 1:25 pm

      Hi David,

      Not sure what you mean about the images being spread out on the page. I haven’t included any CSS changes in these steps to alter the appearance of images, so it might be something particular to your site.
      Also I didn’t intend to allow users to edit galleries with this solution – it is merely meant to restrict galleries in the front end to only display for particular users.

      Hope that clears things up.


      • David says:
        June 21, 2013 at 6:59 pm

        Hi Jo,

        Having re-read your post I obviously saw more than was actually there. A bit of a ‘Senior Moment’ perhaps lol
        I will check with ‘Weaver’ to see if they can offer a solution to the display problem.

        Thank you for your response



        • David says:
          June 21, 2013 at 7:15 pm

          Just as a thought, is it possible that you could post a link or a screenshot so that I can see how it should/could look?


  • John Weekes says:
    August 26, 2013 at 4:23 am

    Good Evening. May I say first of all that this is indeed a great post and is fairly easy to follow however I am currently struggling with the steps outlined in step 3 – Create a page where your restricted galleries will display. Mine is called ‘Client Area’. In the page content, I want to tell NGG which album of galleries to display, so I use the shortcode:

    [album id=2 template=client]
    This album will contain all of the client galleries to which I want to restrict access. By specifying the template to use, we can take advantage of the easy customisability of NextGEN Gallery and create a custom template which will display only the right galleries within this album for the current user.

    Currently all of my clients have there own galleries and not albums under one gallery like I believe you have suggested above, if so how do I go about protecting each individual gallery depending on the user that is logged in. Also I further don’t understand how you’ve obtained the client template outlined in the shortcut above and would therefore be grateful if you could indeed point me in the right direction and provide me with a solution.

    Many Thanks


    • admin says:
      August 26, 2013 at 1:39 pm

      Hi John, thanks for your comment.

      In my setup, each client has their own gallery as well. All of these client galleries are contained within one album in NGG, and are tied to a particular user using the ‘Restrict to user’ field created in step 1.
      I give the code for the client template in step 4 – you just need to copy and paste it as step 4 explains.

      Hope that clears things up!


  • Andrew says:
    November 21, 2013 at 6:55 am

    I love this solution but I have a template applied to the galleries that allows downloading images as a zip, is there a way to use both templates?


    • admin says:
      November 25, 2013 at 8:40 pm

      You can certainly create a template that allows both downloading images as a zip and restricting galleries to certain users. Just make sure you include line 27 from step 4, which compares the current user viewing the gallery to the username specified in the gallery settings, in your existing template.


  • Travis says:
    April 2, 2014 at 3:39 pm

    I have this working on my site; the users see a nice filtered list of galleries. If they click on the thumbnail, it takes them to their gallery. I noticed if you copy the gallery URL, you can still access that it from a different user account (other than the ‘Restrict to User’ account). Any suggestions for how to overcome this?


  • Herve Bernier says:
    April 10, 2014 at 9:40 pm

    I do everything as you say, but when i go to the private album page, i have the message [Not a valid template]
    I am using WP 3.8.2, and NGGalery 2.0.61,
    Please note that i a new to WP and the directory wp-content/plugins/nextgen-gallery/view does not exist


    • admin says:
      April 10, 2014 at 10:02 pm

      Hello. This post was written for NGG version 1.9.13 and below. After this version they changed the plugin significantly and this technique will probably not work as is without making some tweaks, sorry.


  • rajjo says:
    September 9, 2014 at 8:17 pm

    Thanks for the post.It helps me a lot.Is there any way to include videos for the same concept?


  • Pingback: NextGEN Gallery: Galerien für bestimmte Benutzer freigeben

  • tushar says:
    August 18, 2015 at 8:20 pm

    Hi ! I don’t know why the above code is not working for me. I did each and every step as you mentioned. Still i am not getting the restricted page properly i.e. the Client Area page shows everything to every user. Can you help ?


Leave a Reply

Your email address will not be published. Fields marked with * are required

Copyright © 2010 - 2015 JebWeb Solutions

  • facebook
  • linkedin
  • google
  • rss