Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Password Protect Archive and Single Posts for Custom Post Type

Is there an easy way to password protect an archive and single posts of a custom post type?

I found this article on password protecting single posts, but am still lost on the archive loop. I would want it only display the password box until the user has logged in.

https://wordpress.stackexchange.com/questions/4952/forcing-all-posts-associated-with-a-custom-post-type-to-be-private

Thanks,

like image 831
christian Avatar asked Jan 15 '23 08:01

christian


2 Answers

The only way I've found to quickly password-protect the archive is by creating a template that retrieves the custom post type data and associating it with a page that can be password protected. http://codex.wordpress.org/Page_Templates

Once that page is password protected, you find the post ID to apply it to the single-{your_custom_post_type}.php like so:

<?php
if ( !post_password_required('{protected_post_id}') ) : ?>

//protected content here

<?php else:
    //show the password form of the protected page
    echo get_the_password_form('{protected_post_id}');                 

endif; ?>

This saves you from having to password-protect every post under your custom post type.

like image 91
leese Avatar answered Apr 28 '23 12:04

leese


For single pages you could just edit single.php and add something along the lines of:

<?php

if ( is_user_logged_in() ) {
  // Show Post to Logged in User
} 
else {
  //Show password field
}

?>

If like you mentioned you are using a custom post type or an archive template you could apply the same method as above to single-[custom-post-type-name].php or archive.php

like image 39
MÖRK Avatar answered Apr 28 '23 11:04

MÖRK