Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsuar/04ca665e586fed6f6d38df65ef28d911 to your computer and use it in GitHub Desktop.
Save jsuar/04ca665e586fed6f6d38df65ef28d911 to your computer and use it in GitHub Desktop.
if ( !post_password_required() ) {
// Display your product
} else {
echo get_the_password_form();
}
// via https://wordpress.org/support/topic/protect-a-product-by-password
function password_visibility(){
$post = get_post();
if(!is_admin() && !empty($post->post_password)):
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash( 8, true );
$expire = apply_filters( 'post_password_expires', time() + 10 * DAY_IN_SECONDS );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-postpass_' . COOKIEHASH, $hasher->HashPassword( wp_unslash( $post->post_password ) ), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
wp_safe_redirect( wp_get_referer() );
endif;
}
add_action( 'pre_get_posts' , 'password_visibility' );
// via https://wordpress.org/support/topic/bipass-password-protected-posts-or-pages-or-woocommerce-products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment