0

With a WordPress PHP template - I what doing an if statement to check if user is logged in, if he is then I want to show an email form and if he's not I want to ask users to login.

<?php if ( is_user_logged_in() ) {
    <div role="tabpanel" class="tab-panel active" id="nomessage">
        <div class="row">
            <div class="col-lg-12">
                <h4>You have to be <a href="/login/" target="_blank">logged in</a> to send a message</h4>
            </div>
        </div>
    </div>
} else {
    <div class="tab-content">
    <?php if($classieraToAuthor == 1){?>
    <div role="tabpanel" class="tab-pane active" id="message">
    <!--ShownMessage-->
    <?php if(isset($_POST['submit']) && $_POST['submit'] == 'send_message'){?>
        <div class="row">
            <div class="col-lg-12">
                <?php if($hasError == true){ ?>
                <div class="alert alert-warning">
                    <?php echo $errorMessage; ?>
                </div>
                <?php } ?>
                <?php if($emailSent == true){ ?>
                <div class="alert alert-success">
                    <?php echo $classieraContactThankyou; ?>
                </div>
                <?php } ?>
            </div>
        </div>
        <?php } ?>
        <!--ShownMessage-->
        <form method="post" class="form-horizontal" data-toggle="validator" name="contactForm" action="<?php the_permalink(); ?>">
            <div class="form-group">
                <label class="col-sm-3 control-label" for="name"><?php esc_html_e('Name', 'classiera') ?> :</label>
                <div class="col-sm-9">
                    <input id="name" data-minlength="5" type="text" class="form-control form-control-xs" name="contactName" placeholder="<?php esc_html_e('Type your name', 'classiera') ?>" required>
                </div>
            </div><!--name-->
            <div class="form-group">
                <label class="col-sm-3 control-label" for="email"><?php esc_html_e('Email', 'classiera') ?> :</label>
                <div class="col-sm-9">
                    <input id="email" type="email" class="form-control form-control-xs" name="email" placeholder="<?php esc_html_e('Type your email', 'classiera') ?>" required>
                </div>
            </div><!--Email-->
            <div class="form-group">
                <label class="col-sm-3 control-label" for="subject"><?php esc_html_e('Subject', 'classiera') ?> :</label>
                <div class="col-sm-9">
                    <input id="subject" type="text" class="form-control form-control-xs" name="subject" placeholder="<?php esc_html_e('Type your subject', 'classiera') ?>" required>
                </div>
            </div><!--Subject-->
            <div class="form-group">
                <label class="col-sm-3 control-label" for="msg"><?php esc_html_e('Message', 'classiera') ?> :</label>
                <div class="col-sm-9">
                    <textarea id="msg" name="comments" class="form-control" placeholder="<?php esc_html_e('Type Message', 'classiera') ?>" required></textarea>
                </div>
            </div><!--Message-->
            <?php 
                $classieraFirstNumber = rand(1,9);
                $classieraLastNumber = rand(1,9);
                $classieraNumberAnswer = $classieraFirstNumber + $classieraLastNumber;
            ?>
            <div class="form-group">
                <div class="col-sm-9">
                    <p>
                    <?php esc_html_e("Please input the result of ", "classiera"); ?>
                    <?php echo $classieraFirstNumber; ?> + <?php echo $classieraLastNumber;?> = 
                    </p>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label" for="humanTest"><?php esc_html_e('Answer', 'classiera') ?> :</label>
                <div class="col-sm-9">
                    <input id="humanTest" type="text" class="form-control form-control-xs" name="humanTest" placeholder="<?php esc_html_e('Your answer', 'classiera') ?>" required>
                    <input type="hidden" name="humanAnswer" id="humanAnswer" value="<?php echo $classieraNumberAnswer; ?>" />
                    <input type="hidden" name="classiera_post_title" id="classiera_post_title" value="<?php the_title(); ?>" />
                    <input type="hidden" name="classiera_post_url" id="classiera_post_url" value="<?php the_permalink(); ?>"  />
                </div>
            </div><!--answer-->
            <input type="hidden" name="submit" value="send_message" />
            <button class="btn btn-primary btn-block btn-sm sharp btn-style-one" name="send_message" value="send_message" type="submit"><?php esc_html_e( 'Send Message', 'classiera' ); ?></button>
        </form>
    </div>
};
?>

First, I have added an if statement to check whether the user is logged in or not, if he is the first element is shown where is tells the user it has to be logged in to send a message. Then, if he is logged in a message form is shown instead. Cannot get it to work, would appreciate all help a lot! Thanks in advance!

1
  • What is not working exactly ? Commented Jan 27, 2018 at 11:10

1 Answer 1

1

You couldn't mix PHP and HTML like this. You're missing to close PHP tag ?> before to write HTML.

EDIT As pointed by @mmm, your test is the inverse of what you want. You have to negate your test using the character !.

Try this :

<?php if ( ! is_user_logged_in() ) : ?>
    <div role="tabpanel" class="tab-panel active" id="nomessage">
        <div class="row">
            <div class="col-lg-12">
                <h4>You have to be <a href="/login/" target="_blank">logged in</a> to send a message</h4>
            </div>
        </div>
    </div>
<?php else: ?>
    <!-- other HTML -->
<?php endif; ?>

Or :

<?php if ( is_user_logged_in() ) { ?>
    <div role="tabpanel" class="tab-panel active" id="nomessage">
        <div class="row">
            <div class="col-lg-12">
                <h4>You have to be <a href="/login/" target="_blank">logged in</a> to send a message</h4>
            </div>
        </div>
    </div>
<?php } else { ?>
    <!-- other HTML -->
<?php } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

you inverted the if condition, it is !is_user_logged_in() then => ask to login. "you" is for Viktor Fonster and Syscall ;)
@Syscall managed to get the second one to work! Thanks a lot, super appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.