• Is there a function or an argument that I can include in my code that would automatically password protect my posts in a certain category. I am trying to eliminate having to select password protect and enter a password. In my category these will all be password protected with the same password.

    I am using a plug-in that will allow all password-protected to automatically have the same password. So I just need to figure out code that will make all posts in a certain category password-protected.
    The reason I am trying to eliminate this step of having to go in and select password protect and enter a password is because I going to post via e-mail. If can’t eliminate these steps then after I e-mail my post I have to log into WordPress and make these changes. It defeats the purpose of posting via e-mail.

    I did find this function <?php post_password_required( $post ); ?> in the codex. I am not sure how to use it or even where to place it.
    I thank you in advance for any help.

Viewing 15 replies - 1 through 15 (of 24 total)
  • This is completely untested, and I’m not sure if it’ll even be useful but you could give it a try and let us know. The idea I had was password protect a post outside the category. Have users validate through that post, which should set a cookie. If the password matches, it lets you through. If not it redirects you back to the post.

    Add this to your theme functions.php file to test.

    add_action( 'init', 'category_protect' );
    
    if ( ! function_exists( 'category_protect' ) )
    {
    	function category_protect() {
    
    		global $post;
    
    		// edit these
    		$protected_post = '123';
    		$protected_categories = array(3);
    		// stop editing
    
    		$protected = get_post( $protected_post );
    
    		if ( in_category( $protected_categories ) && stripslashes( $_COOKIE['wp-postpass_' . COOKIEHASH] ) != $protected->post_password )
    		{
    			// you shall not pass! - redirect to the protected post
    			wp_redirect( wp_get_permalink( $protected_post ) );
    		}
    
    	}
    }
    Thread Starter astima

    (@astima)

    Thank you very much. I will try it out and let you know how it goes.

    Thread Starter astima

    (@astima)

    quick question

    I assume I replaced the number three with category ID.
    $protected_categories = array(3);

    but what do I replace the 123 with?
    $protected_post = ‘123’;

    Right. The category to protect would be ID 3 in the example, and the protected post (outside that category) would be post # 123.

    Thread Starter astima

    (@astima)

    Thank you for the function, but I couldn’t get it to work. It may have worked and I am doing something wrong.

    Is there a way to write a function that says basically if a post is in x category, then password-protected the post?

    if ( in_category (12)…

    I’ve modified the code after running a test on my local wp. Here’s how I set this up.

    1. I created a protected post, and saw it was post #2463. I entered that below in the $protected_post variable. I also made sure this post was not in the category I will be protecting.

    2. I used the category slug for the category I want to protect. In this case it is ‘society-culture’. I put this in $protected_categories.

    When I visit a post in that category, it redirects me to the protected post so that I can enter the password. After I entered a correct password, I can access posts in that category.

    add_action( 'wp', 'category_protect' );
    
    if ( ! function_exists( 'category_protect' ) )
    {
    	function category_protect() {
    
    		global $post;
    
    		if ( ! is_single() ) return false;
    
    		// edit these
    		$protected_post = '2463';
    		$protected_categories = array('society-culture');
    		// stop editing
    
    		$protected = get_post( $protected_post );
    
    		if ( in_category( $protected_categories, $post->ID ) && stripslashes( $_COOKIE['wp-postpass_' . COOKIEHASH] ) != $protected->post_password )
    		{
    			// you shall not pass! - redirect to the protected post
    			wp_safe_redirect( get_permalink( $protected_post ) );
    		}
    
    	}
    }

    It might be possible to add the password form in there instead of a redirect. I haven’t gotten that far with the research and had already spent a bit of time on it. I was kind of hoping someone else might know and would chime in.

    I wonder if you could just password protect a category page? Only make the category available through that page..

    Ok I think I got it. Try this one, but you enter the password into this function. What this does is replace your content with the password form if the user doesn’t have a valid password cookie.

    add_action( 'wp', 'category_protect' );
    
    if ( ! function_exists( 'category_protect' ) )
    {
    	function category_protect() {
    
    		global $post;
    
    		if ( ! is_single() ) return false;
    
    		// edit these
    		$password = '123';
    		$protected_categories = array('society-culture');
    		// stop editing
    
    		if ( in_category( $protected_categories, $post->ID ) && stripslashes( $_COOKIE['wp-postpass_' . COOKIEHASH] ) != $password )
    		{
    			$post->post_content = get_the_password_form();
    		}
    
    	}
    }
    Thread Starter astima

    (@astima)

    I just want to make sure I do it correctly, where do you enter the password function?

    The code above goes into your functions.php file, in your theme folder. It should replace the old code you used before that wasn’t working, so make sure you’ve removed the old code.

    Edit the code
    1. Change ‘123’ to the password you want to protect category posts.
    2. Change ‘society-culture’ to the name of your own category slug for the category you want to protect.

    That’s it.

    This is a cool idea, I’ll try it too. But If it doesn’t work for you, I tried the password proteced category page. Seems there’s no way to access a category page to make it password protected, and redirect doesn’t work since the category is not really protected, only the page that refirects to it.

    However I imagine it could be done with a custom page template for the category. You could prevent previous-next post nav from running into ither categories, either with a plugin that gets post nav by refering post (keeping all other posts in that category) or not use previous-next and just have a category posts widget, conditional for that category.

    Another possibility for a plugin would be to hook into the publish_post, and if the post is published in a category you wish to protect, it automatically protects the post with the password. Disadvantage: if you change your mind at some point later, you have all those posts in the category that are protected. You would have to manually go through and unprotect them.

    The code above in functions.php works, and is better in that you could toggle it on or off globally. Be sure you’re using the code in the post linked below and not the broken code in the posts early in this topic.
    http://wordpress.org/support/topic/automatically-password-protecting-posts?replies=12#post-2635048

    You know, I didn’t look yet but maybe there were some good category protection plugins already in the repository.

    thanks, can’t wait to try!

    And I have yet to find a password protect category plugin. Private Suite is one that makes a category private (each user only see’s their own posts) , but not password protected. I used a very robust membership plugn called S2member, but I don’t recal if it could do this. I used it for another purpose.

    I assume there must be a reason we don’t see at least a few plugins for this..? I’ve seen many requests for it.

    I just checked the info on the home page of the mentioned “s2member” plugin, and it says you can “Can protect individual WordPress® Categories, including content within.”

    Now I don’t know what that says, I’d like a simple password-protection too, without forcing users to register to see content.
    This is for a family site where they’ll use some birthday or a pet’s name as a password so that only friends and family can see certain pics.

    Is it hard to hook in to the outputting process to determine if a post is in a certain category or not?

    i’m currently using a plugin called “restrict categories” it allows access to cateories by user role or user name.
    However, you would have to assign the roles, or assign categories per user, and users would have to log in.

    you could however pasword protect pages, and you could make a category page. However the category page would have to be static, not the dynamically generated type.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘automatically password protecting posts’ is closed to new replies.