WordPress.org

Ready to get started?Download WordPress

Forums

automatically password protecting posts (25 posts)

  1. astima
    Member
    Posted 2 years ago #

    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.

  2. Beer
    Member
    Posted 2 years ago #

    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 ) );
    		}
    
    	}
    }
  3. astima
    Member
    Posted 2 years ago #

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

  4. astima
    Member
    Posted 2 years ago #

    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';

  5. Beer
    Member
    Posted 2 years ago #

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

  6. astima
    Member
    Posted 2 years ago #

    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)...

  7. Beer
    Member
    Posted 2 years ago #

    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.

  8. deepbevel
    Member
    Posted 2 years ago #

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

  9. Beer
    Member
    Posted 2 years ago #

    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();
    		}
    
    	}
    }
  10. astima
    Member
    Posted 2 years ago #

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

  11. Beer
    Member
    Posted 2 years ago #

    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.

  12. deepbevel
    Member
    Posted 2 years ago #

    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.

  13. Beer
    Member
    Posted 2 years ago #

    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.

  14. deepbevel
    Member
    Posted 2 years ago #

    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.

  15. mores
    Member
    Posted 1 year ago #

    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?

  16. deepbevel
    Member
    Posted 1 year ago #

    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.

  17. mores
    Member
    Posted 1 year ago #

    I'll check it out, thanks for the suggestion.
    Maybe I can take some parts and put together a plugin myself.

  18. deepbevel
    Member
    Posted 1 year ago #

    keep my previous comment in mind:

    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.

    I was talking about wp category pages, you can do it with a custom page template and a category query, but you'd need one for each user or role, or "pet name" ect.

    and, this is only so "secure" because the category is not protected, only the page. However should be good enough for your purpose. Juat don't allow access to categories except throgh the pages.

  19. mores
    Member
    Posted 1 year ago #

    Using a category-x.php file to password-protect a category is no good, since you can bypass it by going to a post directly.

    I could manually make the password appear in post.php (if category ...) and have the index.php check for a post's category and either display the excerpt or a "password-protected" message.

    What I want is a plugin, since I don't want to implement this in every theme that I use, and sometimes I buy themes that are pretty un-friendly when it comes to edits (one-line-code and other such nonsense) so I'm mainly looking for a way to hook in to the display process and get that to check for category X.

    Thanks for the input, though. If you know how to hook in to the "displaying" process of wordpress to insert a "if category" thing, then please tell me :)
    My previous 2 plugins were developed a long time ago and I seem to have forgotten a whole lot since then :)

  20. deepbevel
    Member
    Posted 1 year ago #

    the category is not protected, only the page. However should be good enough for your purpose

    yes, not truely secure but works for keeping content seperated per user or user level. guess I wouldn't anticipate a need for strict security for pet posts, haha..

    I may not quite understand where you're going with this, I've never made a plugin.. not sure what it entails.

    If you are just talking about

    <?php if(is_category('ID'));?>
    (output code)
    <?php else {?>
    (something else)
    <?php } ?>

    otherwise, no sure what you mean by "displaying process"

  21. Beer
    Member
    Posted 1 year ago #

    Did my latest post with sample code not work? Did anyone try it?

  22. deepbevel
    Member
    Posted 1 year ago #

    I can't remember if I did or not, it was a while ago and I think I ended up not needing it.
    I don't think there's a plugin that checks posts for private categories like that. I'll have to test it just for the heck of it.

    Maybe it will help mores..?

  23. mores
    Member
    Posted 1 year ago #

    Here's the first bit of what I'm working on.

    function cat_pass($content) {
    	global $wpdb;
    	global $post;
    
    	$cat_pass_cat = get_option('cat_pass_cat');
    	if (!$cat_pass_cat) : $cat_pass_cat = 107; endif;
    	$post = get_post($id);
    	if ($post->post_type=='post') :
    		if ( in_category(107, $post) ) : $content = "Nope. You no see dis!"; endif;
    	endif;
    	return $content;
    }
    
    add_filter( 'the_content', 'cat_pass' );

    All it does is check if a post is in a certain category, then replaces the content (and excerpt) with a text.

    Next step: upgrade this to 1) create a back-end to set password and category to protect and 2) display the password field instead of my nifty little text :)

    Final step: use cookies to save the password so you don't need to enter it all the time.

    What's good about using a filter is that it cannot be bypassed by going around a category-page or something - each post in this category will only show its content when the password is given.

    EDIT: this bit can be used in a functions.php too. I'll just make it a plugin for the ease of use and a nice back-end. Otherwise you need to manually insert the right category-id.

  24. deepbevel
    Member
    Posted 1 year ago #

    right, and it looks like Beer's code may take care of #2..?

  25. mores
    Member
    Posted 1 year ago #

    Update for those who care :) :
    Plugin now replaces protected content with a password form. If you enter the password, you get to see the content. Cookie is set so you don*'t have to enter it all the time.
    Next step: back-end category selector.

    Beer's code unfortunately wasn't helpful for me, because it works on another level and uses the post-password feature.

Topic Closed

This topic has been closed to new replies.

About this Topic