wp-plugin或任何用于wordpress中特定类别的密码保护的代码

时间:2012-08-16 08:27:27

标签: wordpress wordpress-plugin

  1. 我想对我的特定类别应用密码保护 一个论坛,我看到了这个代码。但是,它对我不起作用。
  2. 在我的主题的function.php中包含此代码:

    global $post; if ( ! is_single() ) return false; // edit these $password = 'your password'; $protected_categories = array('category-slug-name'); // stop editing if ( in_category( $protected_categories, $post->ID ) && stripslashes( $_COOKIE['wp-postpass_' . COOKIEHASH] ) != $password ) { $post->post_content = get_the_password_form(); } } } ?>
  3. 有关此事的任何想法。对我来说有点紧急。如有任何想法请回复我的问题。谢谢。

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

我硬编码了我的类别页面以获取密码保护 -

<?php 
global $wpdb;
//create a table named pass_protect_cat with password field in it.
$db_pass = $wpdb->get_results("SELECT password FROM pass_protect_cat",ARRAY_N); 
$db_pass1=array_shift( $db_pass );
$db_password=array_shift($db_pass1);
$cat_pass_entered=$_POST['pass'];
$displayform=true;
if ($_POST['submitted'] )
{
    if((strcmp($db_password,$cat_pass_entered)) == 0)
    {
        $displayform=false;
    ?>
        <?php while (have_posts()) : the_post(); ?>
            <p align="left" class="txt_small"><?php the_content().'<br/>'; ?></p>
        <?php endwhile; ?>
    <?php
    }
    else
    {
        echo '<b>'."Wrong Password".'</b>';
    }
}
if($displayform)
{

&GT;

<p align="left" class="txt_small" id="Message">These category posts are Password Protected.<br/>Enter the password to view the contents.</p>
                <form name="form1" id="form1" method="post" action="<?php password_protect_cat.php ?>" >
                    <label for="password">Password</label>
                    <input type="password" name="pass" id="pass" value="">
                    <input type="submit" name="submitted" id="submitted" value="Submit">
                </form>

<?php } ?>