Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Password Protect Taxonomy Terms and All Posts in the Terms

Posted on January 1, 2012 by Pippin in Advanced, Member Restricted, Subscriber Only, Taxonomies, Tutorials, WordPress Admin / Dashboard, Writing Plugins 16 Comments
Home» Tutorials » Advanced » Password Protect Taxonomy Terms and All Posts in the Terms
Tweet
Love It - 0

A few weeks ago, AJ Clarke of WP Explorer released a really slick photography theme called Fotos. Aside from just being a great theme, it also has a great feature that allows users to password protect their photo foto galleries. While developing the theme, AJ, who is a good friend of mine, asked me for help on writing the functions that would allow him to give his users this password protection feature. Over a couple of days, with lots of tweaking, we came up with a pretty solid system that worked really well. It uses the taxonomy meta system built into WordPress, which I showed you how to use with Adding Custom Meta Fields to Taxonomies, and allows you to password protect every post inside of particular taxonomy terms by setting one single password on the taxonomy term page. Now, I’d like to show you how it works so you can build your own password protected taxonomies.

Due to the nature of how this system works, I’m going to show you how to password protect the regular Post Category taxonomy, but you could very easily adapt it to any custom taxonomy. AJ’s theme uses a custom taxonomy called “album_cats”.

Unlike many of my other tutorials, there will not be a simple plug-and-play plugin to use at the end, but rather a selection of snippets and functions that you will use in your theme’s template files. It would be possible to extend the results I give you here to a complete plugin that works out of the box, but it’s beyond the scope of this tutorial.

You must be logged in and have an active premium membership to view the rest of this content. Register or login from the sidebar.

Tweet Follow @pippinsplugins
custom meta, custom taxonomy, meta, taxonomies, taxonomy

16 comments on “Password Protect Taxonomy Terms and All Posts in the Terms”

  1. chrispian says:
    January 3, 2012 at 9:59 am

    I’m logged in but still can’t see the rest of the content, fyi.

    Reply
    • Pippin says:
      January 3, 2012 at 10:00 am

      This tutorial is restricted to paid subscribers. You will need to add a subscription to your account if you want to view the rest of this post.

  2. chrispian says:
    January 3, 2012 at 10:05 am

    Gotcha. Thanks!

    Reply
  3. smartmediaas says:
    January 12, 2012 at 7:22 am

    Hi Pippin..

    Will the ‘enter password’ display when going to archive/taxonomy/term? So you can’t see the list of posts in term without password..

    thanks

    Reply
    • Pippin says:
      January 12, 2012 at 11:06 am

      No, the functions are designed for individual posts. It could be modified pretty easily, however, to hide all posts in a term.

  4. up84 says:
    April 3, 2012 at 3:31 am

    I think it might be better to use something like this: add_filter(‘the_content’, ‘check_the_post’);
    We can use conditional statements then, such as: is_single() and is_index(). What do you think?

    Reply
    • Pippin says:
      April 3, 2012 at 9:43 am

      That is better for one scenario: when you only care about hiding the post content. When I built this, AJ really wanted to hide everything, including titles, dates, and post thumbnails. In order to do that, an extra function had to be added to the loop, as in my example.

    • up84 says:
      April 3, 2012 at 10:25 am

      I see. Thanks for reply.

  5. scimon says:
    June 15, 2012 at 11:17 am

    This is a very spiffy plug-in, but it blocks viewing of the preview when drafting a new post once a protected category checkbox is ticked. What would be the best way to allow previews, other than to remember to not check the box until you need to remember to check the box before posting? thanks in advance!

    Reply
  6. scimon says:
    June 15, 2012 at 12:34 pm

    Here’s a little something I whipped up to easily list all passwords that are set. It could be more robust (check blog_id in wp_options, specify the taxonomic type in the table and URL) but it works for our purposes and could be a good starting point for someone.


    add_action('admin_menu', 'my_pass_list_enable');
    function my_pass_list_enable(){
    #Place in Posts menu
    add_posts_page('Taxonomy Passwords', 'Taxa Passwords', 'publish_posts', 'my-taxa-pass', 'my_pass_list');
    }
    function my_pass_list(){
    global $wpdb;

    $taxametas = $wpdb->get_results('SELECT option_name, option_value FROM wp_options WHERE option_value LIKE "%term_pass%"');

    foreach ($taxametas as $taxameta) {
    preg_match('/(\d+)$/', $taxameta->option_name, $taxid);
    $opts = unserialize($taxameta->option_value);
    $cat = get_category($taxid[0]);
    $taxainfo[$cat->name] = array('slug'=>$cat->slug,
    'pass'=>$opts['term_pass']);
    }

    asort($taxainfo);
    echo 'TermPassword';
    while( list($key, $val) = each($taxainfo) ){
    printf('%s%s', $val['slug'], $key, $val['pass']);
    }
    echo '';
    }

    Reply
    • scimon says:
      June 18, 2012 at 11:08 am

      Insert the following before return true on line 79 (no valid password yet) to allow the post author to preview drafts/pending posts.

              #punt if draft/preview & am author
              $chk = get_post($post_id);
              if( ($chk->post_status == 'draft' || $chk->post_status == 'pending') &&
                  $chk->post_author == wp_get_current_user()->ID
                  )
                return false;
      
    • Pippin says:
      June 18, 2012 at 12:46 pm

      Yep, that should work.

    • scimon says:
      June 22, 2012 at 3:27 pm


      #Generalize get_category above to allow use on tags or custom taxa
      $cat = get_term($taxid[0], 'category');

  7. scimon says:
    June 15, 2012 at 12:39 pm

    Doh, your site seems to process the accepted HTML tags other than code before code (and escaping the contents thereof) :-/

    Reply
    • Pippin says:
      June 15, 2012 at 2:10 pm

      You need to paste the code inside of PRE tags. Like this:

      <pre lang=”php” line=”1″>
      // code here
      </pre>

  8. Danice says:
    August 23, 2012 at 8:49 am

    Awesome you should think of smotehing like that

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Adding Custom Meta Fields to Taxonomies
  • ECPT: Filter by Taxonomy Add-on
  • Automatically Displaying Meta Field Values in Easy Content Types

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

the_content shortcodes contextual help get_user_meta featured register_setting add_options_page attachments meta box Rémi Corson add_shortcode Tom McFarlin wp_enqueue_script attachment Related posts image plugin forms login short codes do_action mail chimp authors apply_filters comments recent posts post types bbpress short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins