Session is recommended but doesn\'t work when you\'re caching the content of your blog (using for example these caching plugins), you need to reenter the password each time you launch your browser, this is the safer process. Cookie will always work, you don\'t need to type the password each time you want to access to the resource, but this is the less secure way.', 'access-category-password' ); ?>
acpwdpass.', 'access-category-password' ); ?>
/>
'.__('Allowed tags:', 'access-category-password').' '. allowed_tags() ?>
'.__('Allowed tags:', 'access-category-password').' '. allowed_tags() ?>
classes you can see in the code below:', 'access-category-password' ) ?>
.
<div class="acpwd-container">
 <p class="acpwd-added-excerpt">Here comes the Excerpt if enabled…</p>
 <p class="acpwd-info-message">This content has restricted access, please type the password below and get access.</p>
 <form class="acpwd-form" action="" method="post">
 <input class="acpwd-pass" type="password" name="acpwd-pass">
 <input class="acpwd-submit" type="submit" value="Get access">
 </form>
 <p class="acpwd-error-message">Sorry, but this is the wrong password.</p>
</div>
function acpwd_head_styling() { $options = get_option( 'acpwd_settings_options' ); $acpwd_default_css = '* Custom css for Access Category Password form */ .acpwd-container { } .acpwd-added-excerpt { } .acpwd-info-message { } .acpwd-form { } .acpwd-pass { } .acpwd-submit { } .acpwd-error-message { color: darkred; }'; $output_styling = (isset($options['output_styling']) && $options['output_styling'] != '') ? '' : ''; echo $output_styling; } add_action( 'wp_head', 'acpwd_head_styling' ); /* Start and destroy sessions */ add_action('init', 'acpwdStartSession', 0); add_action('wp_logout', 'acpwdEndSession'); add_action('wp_login', 'acpwdStartSession', 0); function acpwdStartSession() { // Start session only if this is the way we want to authenticate... $acpwd_options = get_option('acpwd_settings_options'); $protectway = (isset($acpwd_options['protectway'])) ? $acpwd_options['protectway'] : 'session'; if (!session_id() && $protectway == 'session') { session_start(); } } function acpwdEndSession() { unset($_SESSION['acpwd_session']); session_destroy (); } /* Validation of the password */ function acpwd_mdp_check() { // The form has been submited if(isset($_POST['acpwd-pass'])) { // Checking password $acpwd_options = get_option('acpwd_settings_options'); $cookieexp = (isset($acpwd_options['cookieexp'])) ? $acpwd_options['cookieexp'] : 311040000; $protectway = (isset($acpwd_options['protectway'])) ? $acpwd_options['protectway'] : 'session'; if(crypt($_POST['acpwd-pass'], $_POST['acpwd-pass']) == $acpwd_options['password']) { if($protectway == 'session') { setcookie('acpwd_cookie', '', time() - 3600, '/'); if (isset($_COOKIE['acpwd_cookie'])) unset($_COOKIE['acpwd_cookie']); $_SESSION['acpwd_session'] = 1; } else { if (isset($_SESSION['acpwd_session'])) { unset($_SESSION['acpwd_session']); session_destroy (); } if (!isset($_COOKIE['acpwd_cookie'])) setcookie('acpwd_cookie', 1, time() + $cookieexp, '/'); $_POST['acpwd_cookie_ok'] = true; } } else { $_POST['acpwd-msg'] = ($acpwd_options['error_message'] != '') ? '' : ''; if($protectway == 'session') { $_SESSION['acpwd_session'] = 0; } else { setcookie('acpwd_cookie', 0, time() - 3600, '/'); unset($_COOKIE['acpwd_cookie']); $_POST['acpwd_cookie_ok'] = false; } } } } add_action('init', 'acpwd_mdp_check', 1); /* Displaying the password form or the feed replacement sentence for post or excerpt content */ function acpwd_frontend_changes($content) { if (is_user_logged_in()) { $user = wp_get_current_user(); $u_roles = $user->roles; $u_role = $u_roles[0]; } else { $u_role = 'not logged in'; } $acpwd_options = get_option('acpwd_settings_options'); $protectway = (isset($acpwd_options['protectway'])) ? $acpwd_options['protectway'] : 'session'; $impacted_categories = (isset($acpwd_options['impacted_categories'])) ? $acpwd_options['impacted_categories'] : array(); $allowed_users = (isset($acpwd_options['allowed_users'])) ? $acpwd_options['allowed_users'] : array(); $excerpt_before_pass = (isset($acpwd_options['excerpt_before_pass'])) ? $acpwd_options['excerpt_before_pass'] : 0; $excerpt_before_pass_length = (isset($acpwd_options['excerpt_before_pass_length'])) ? $acpwd_options['excerpt_before_pass_length'] : 10; $excerpt_before_pass_ending_string = (isset($acpwd_options['excerpt_before_pass_ending_string'])) ? $acpwd_options['excerpt_before_pass_ending_string'] : ' […]'; $added_excerpt = ($excerpt_before_pass == 1) ? '
'.wp_trim_words( $content, $excerpt_before_pass_length, $excerpt_before_pass_ending_string ).'
'."\n" : ''; $info_message= (isset($acpwd_options['info_message']) && $acpwd_options['info_message'] != '') ? '' : ''; $validation_button = (isset($acpwd_options['valid_button_text']) && $acpwd_options['valid_button_text'] != '') ? '' : ''; $input_placeholder = (isset($acpwd_options['input_placeholder'])) ? stripslashes($acpwd_options['input_placeholder']) : ''; if ( in_category($impacted_categories) ) { // For the following test : Session case || Cookie case || Single post case || Allowed users case if (($protectway == 'session' && isset($_SESSION['acpwd_session']) && $_SESSION['acpwd_session'] == 1) || (($protectway == 'cookie' && isset($_POST['acpwd_cookie_ok']) && $_POST['acpwd_cookie_ok'])) || ($protectway == 'cookie' && isset($_COOKIE['acpwd_cookie']) && $_COOKIE['acpwd_cookie'] == 1) || (isset($acpwd_options['only_single']) && $acpwd_options['only_single'] == 1 && !is_single()) || in_array($u_role, $allowed_users)) { $new_content = $content; } else { if (is_feed()) { // Feed content replacement $new_content = stripslashes($acpwd_options['feed_desc_text']) ; } elseif (is_main_query()) { // Post or excerpt content replacement $new_content = 'content###'.$content.'####
'; $new_content .= $info_message."\n"; $new_content .= ''."\n"; if (isset ($_POST['acpwd-msg'])) $new_content .= $_POST['acpwd-msg']; $new_content .= '