Hiding the form once password has been entered. Using WordPress and (!post_password_required())

StackOverflow https://stackoverflow.com/questions/23666927

  •  22-07-2023
  •  | 
  •  

Question

I have a WordPress page which is password protected. It's currently using Custom Fields to display content, so I had to use (!post_password_required()) to hide the area I want.

The code looks like:

<?php if (!post_password_required()) { ?>
<?php the_content(); ?>

<h1>Application Received</h1>

<?php $loop = new WP_Query(array('post_type' => 'applications', 'posts_per_page' => 100)); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>

   <?php the_field('last-name'); ?> <br />
   <a href="mailto:<?php the_field("email"); ?>"><?php the_field("email"); ?></a> <br />
   <?php the_field('organisation'); ?>

<?php endwhile; ?>

<?php } else; { ?>

   <div class="login">
     <h1>SAC Login</h1>
     <?php echo custom_password_form(); ?>
   </div>
</main>
<?php } ?>

So it all works as I expected, but the problem is once you enter a password and the hidden content becomes visibile, the password form stays on the page (underneath the now visible content). I thought that it would disappear once the content appears?

Any thoughts on how I can fix this? Any inputs would be much appreciated.

Was it helpful?

Solution

<?php 
     if (!post_password_required())
     {
          the_content();
          $loop = new WP_Query(array('post_type' => 'applications', 'posts_per_page' => 100));
          while ($loop->have_posts()) : 
              $loop->the_post();
              the_field('last-name');
              the_field("email");
              the_field("email");
              the_field('organisation');
          endwhile;
      } 
      else; 
      {
          echo custom_password_form();
      }
?>

Here's your PHP equivalent page as you can see the else has a semicolon causing the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top