WordPress.org

Ready to get started?Download WordPress

Forums

hiding one page of menu until website terms are accepted (4 posts)

  1. Runtheball
    Member
    Posted 4 years ago #

    I'm working on a WordPress site where I want to hide just one page from the navigation until the visitor agrees to the "terms and conditions". The sidebar has a single checkbox, and submit button, on the homepage for the user to agree to the terms. Once agreed, I want the hidden page to show on the navigation, and the sidebar to disappear for the remainder of that session.

    I think I'm close, but I've been staring at this TOO long, and you know how that goes! Here are the relevant sections (site is myresultz.us/)...

    ...in the Main Index Template...

    <?php
      session_start();
    
      // If session vars aren't set, try to set them with a cookie
      if (!isset($_SESSION['tcagreed'])) {
        if (isset($_COOKIE['tcagreed']) {
          $_SESSION['tcagreed'] = $_COOKIE['tcagreed'];
        }
      }
    
       get_header();
    ?>

    ...in the header...

    <div class="right">
         <?php
            if (isset ($_POST['submit'])) {
              //grab the form data from the POST
              $tcagreed = $_POST['tcagreed'];
              echo '$tcagreed';
    
              if (!empty($tcagreed)) {
                setcookie('tcagreed');
              }
            }
    
            if (isset($_COOKIE['tcagreed'])) {
               wp_page_menu(array('menu_class' => 'nav'));
            }
            else {
               wp_page_menu(array('menu_class' => 'nav', 'exclude' => 'Keywords' ));
            }
         ?>
    </div>

    ...on the home page...

    <form id="tc" action="index.php" method="post">
    <table>
    <tbody>
    <tr>
    <td style="padding-bottom: 8px;"><input type="checkbox" name="tcagreed" value="tcagreed"  />I've read, and agree to, the Terms and Conditions.</td>
    </tr>
    <tr>
    <td><input name="submit" type="submit" value="Submit" /></td>
    </tr>
    </tbody>
    </table>
    </form></div>

    Would greatly appreciate anyone taking the time to give me a fresh perspective on the coding.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're missing a right bracket on this line..

    if (isset($_COOKIE['tcagreed']) {

    Should be.

    if( isset( $_COOKIE['tcagreed'] ) ) {

    Your echo won't output the variable's value.

    echo '$tcagreed';

    Variables aren't read inside single quotes(and you don't need quotes for variables on there own)..

    echo $tcagreed; // Quotes not needed
    //echo "$tcagreed"; // If you're using quotes, then use double quotes
    //echo 'some example text' . $tcaagreeed; // Or! concatenate your strings with your variable.

    Note, when you set the cookie here.

    setcookie('tcagreed');

    You're simply setting the cookie, but with no value, it's usually a good idea to give it a value to(although not strictly necessary).

    setcookie('tcagreed','agreed');

    And a small suggestion for the session start.

    if (!session_id())
    session_start();

    Hope that helps... :)

  3. Runtheball
    Member
    Posted 4 years ago #

    That was helpful, thank you. But I still couldn't get the main issues resolved.

    Now I'm taking a new approach, making the form simpler but still trying to set a cookie when the terms are agreed to (submit button is clicked) and use that cookie to determine which menu to display. Also want the menu display to persist across pages (one menu if terms are accepted, another if not). Form should disappear after terms accepted. I think I've streamlined this considerably, but having a huge mental block and need help.

    The latest code bits...

    header.php...

    <?php
       if (isset($_COOKIE['tcagreed'])) {
           wp_page_menu(array('menu_class' => 'nav'));
       }
       else {
          wp_page_menu(array('menu_class' => 'nav', 'exclude' => 'Keywords' ));
       }
    ?>

    Main index template...

    <?php  
    
      if (isset ($_POST['submit'])) {
         $output_form = false;
         setcookie('tcagreed', 'agreed');
      }
    
      else {
         $output_form = true;
      }   
    
       get_header();
    ?>

    The form, now within index.php...
    <?php if($output_form) {?>
    <div id="sidebar">
    <!--[if IE ]-->
    <div id="IEroot">
    <!--[endif]-->
    <form id="tc" action="index.php" method="post">
    <table>
    <tbody>
    <tr>
    <td><input name="submit" type="submit" value="I accept the Terms and Conditions" /></td>
    </tr>
    </tbody>
    </table>
    </form>
    <p class="vertspacer"></p>
    <!--[if IE ]-->
    </div>
    <!--[endif]-->
    </div>
    <?php }?>

    If I can get the darned form to display, and the menu to change after the form submit button is pushed, I'll figure out the session info myself.

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Print your variables and double check your conditional statements.

    NOTE: Your previous code's cookie and session values were setting for me just fine.

    If your form is inside a function or another file, then you might need to globalise the variable(printing them(variables) out helps spot these problems).

Topic Closed

This topic has been closed to new replies.

About this Topic