woo-commerce password protected products display with out any restriction for some users

I have created some ‘password protected’ Products from the back-end. Now i want to display those ‘Password Protected’ Products to some particular users, with out any password restriction. For the remaining users they should ask password to view the product. Is that possible. Please let me know if any solution.

In short terms, Need to change WordPress visibility options from functions.php

Read More

Thanks,
Satya

Related posts

Leave a Reply

1 comment

  1. After some research on WordPress inbuilt functionality, I got a solution and its working.

    In ‘functions.php’ file i did the following code.

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

    Hope this may help for some others who have the same requirement. I can use this code for some particular users or particular categories,etc..