Support » Fixing WordPress » Password Protect Custom Fields

  • I found this piece of code,but it doesn’t say when you need to replace “protected stuff” with. I tried using the .class of the content I want to include, I tried the custom field name, I even tried wrapping the entire block of code in this, but nothing worked.

    http://wordpress.org/support/topic/hide-sidebar-content-if-page-is-password-protected

    <?php
        if ( !post_password_required() ) {
                echo 'protected stuff';
        }
    ?>

    This is the custom field I’m trying to include in the hidden password protected content.

    <?php $ProjectHeader = get_post_meta($post->ID, 'ProjectHeader', true); if ($ProjectHeader) { ?>
    <div class="ProjectHeader"><?php echo $ProjectHeader; ?></div>
    <?php } else { ?>
    <div class="ProjectHeaderNone">
    <?php the_title(); ?>
    </div>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this 🙂

    <?php
        if ( !post_password_required() ) {
    
    <?php $ProjectHeader = get_post_meta($post->ID, 'ProjectHeader', true); if ($ProjectHeader) { ?>
    <div class="ProjectHeader"><?php echo $ProjectHeader; ?></div>
    <?php }
    
     else { ?>
    
    <div class="ProjectHeaderNone">
    <?php the_title(); ?>
    </div>
        }
    }
    ?>
    Thread Starter alanchrishughes

    (@alanchrishughes)

    Thanks @keithdriscoll . But that seems to just break the page.

    It’s also a little skewed, the if/else is for if there is a custom field entered, else, just show the title. And I want to have both hidden along with the post’s content until the password is entered.

    Thanks for any help you can share.

    This should lead you in the right direction 🙂

    <?php if ( post_password_required() ) { ?>
    
        <form method="post" action="/wp-login.php?action=postpass">
    
            <p>This content is password protected. To view it please enter your password below:</p>
    
            <input type="password" style="margin:10px 0;" size="20" id="pwbox-<?php the_ID(); ?>" name="post_password"/></label><br/>
            <input type="submit" value="Submit" name="Submit"/></p>
    
        </form>
    
    <?php } else { ?>
    
        // PASSWORD IS CORRECT
    
    	<?php $ProjectHeader = get_post_meta($post->ID, 'ProjectHeader', true); if ($ProjectHeader) { ?>
    
    		<div class="ProjectHeader"><?php echo $ProjectHeader; ?></div>
    
    	<?php } else { ?>
    
    	<div class="ProjectHeaderNone">
    
    	<?php the_title(); ?>
    
    		</div>
    
    <?php } ?>
    Thread Starter alanchrishughes

    (@alanchrishughes)

    Thanks again Keith. That still broke the page, but I was able to tweak it a bit and get it working.

    <?php if ( post_password_required() ) { ?>
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <?php $ProjectHeader = get_post_meta($post->ID, 'ProjectHeader', true); if ($ProjectHeader) { ?>
    <div class="ProjectHeader"><?php echo $ProjectHeader; ?></div>
    <?php } else { ?>
    <div class="ProjectHeaderNone">
    <?php the_title(); ?>
    </div>
    
    <?php } ?>
    
    <?php endwhile; ?>
    
    <form method="post" action=" <?php echo site_url(); ?>/wp-login.php?action=postpass">
    
    <input name="post_password" class="ClientPasswordInput"id="' . $label . '" type="password" /><br />
    
    <input type="image" src="<?php echo bloginfo('template_url'); ?>/images/password.jpg" class="ClientSubmit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    
    </form>
    
    <?php } else { ?>
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <?php $ProjectHeader = get_post_meta($post->ID, 'ProjectHeader', true); if ($ProjectHeader) { ?>
    <div class="ProjectHeader"><?php echo $ProjectHeader; ?></div>
    <?php } else { ?>
    <div class="ProjectHeaderNone">
    <?php the_title(); ?>
    </div>
    
    <?php } ?>
    
    <?php the_content(); ?>
    
    <?php endwhile; ?>
    
    <?php } ?>

    Awesome!

    BTW…I’ve never used that function . Glad you got it working 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Password Protect Custom Fields’ is closed to new replies.