0

I'm trying to display text in a button in the header that says "LOG IN" or "LOG OUT" depending on the visitor's status. The button calls a slider action, so I just need to change the text displayed on it.

I have found snippets like this, but they go into index.php, and I don't know how to add them to the button. (In my header.php)

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
};

I think the solution might be to name it in my .js file, and have the action there as well. This is my first attempt, and have no idea if this is the correct way to achieve this.

jQuery(document).ready(function(){
if ( is_user_logged_in() ) {
    document.getElementById("visitorstatus").innerHTML = "LOG OUT";
} else {
    document.getElementById("visitorstatus").innerHTML = "LOG IN";
};
});

And then to add <div id="visitorstatus"> to the button in header.php? Please be kind! I'm new to PHP/JS

ETA header.php

<div class="memberpresswindow">
<button type="button" class="hide-btn">X</button>
<?php echo do_shortcode( "[mepr-login-form]" ); ?>
<?php echo do_shortcode( "[mepr-membership-registration-form id=702]" ); ?> 
<br>
<hr>
<button type="button" class="hide-btn">CLOSE</button> 
</div>
<button type="button" class="show-btn">LOG IN | REGISTER

</button>
            </div>
3
  • Can you please share your header.php where the button is? PHP is definitely the place to do it. JavaScript is kind of a last resort for changing things and the function you are using there, is_user_logged_in(), is a PHP one that won't work in JS.
    – WebElaine
    Jun 18, 2019 at 19:36
  • Also, have you built a child theme yet? Anytime you make changes to a theme they need to be made in a child theme so your changes don't get overwritten when the theme updates.
    – WebElaine
    Jun 18, 2019 at 19:37
  • There is a child theme. The header.php is copied over from the parent; the functions.php is a fresh file. I've added the snippet fr header.php that displays the button. Where it says "LOG IN | REGISTER" is where I'd like to change the static text to conditional text. Jun 18, 2019 at 19:50

1 Answer 1

0

You have the PHP IF statement needed, is_user_logged_in().

Just add your button to that and you should be good. Something this...

<?php if (is_user_logged_in()) {
    echo '<button type="button" class="show-btn">LOG IN</button>';
} else {
    echo '<button type="button" class="show-btn">REGISTER</button>';
}; ?>

Note: You have an extra </div> in your code above.

3
  • That was perfect - THANK YOU! (I have a follow-up question, but if it's not appropriate to add here, please LMK to open a new topic?) When the user is logged in, I'd like to display a shortcode link from the membership plug-in in a button. I successfully added the link, but not sure of the right way to style it in a button? 1st try here: <?php if (is_user_logged_in()) { echo '<button type="button" class="logout-btn"> echo do_shortcode( "[mepr-logout-link]" ) </button>'; } else { echo '<button type="button" class="show-btn">LOG IN | REGISTER </button>'; }; ?> Jun 18, 2019 at 21:23
  • What is your shortcode outputting? An <a> tag? I wouldn't put an anchor inside a button, I guess you can technically put one around the button but I wouldn't do that either. Just show the shortcode if they are logged in, else show the register button (or link). stackoverflow.com/questions/16280684/… Jun 18, 2019 at 21:32
  • Figured it out! <?php if (is_user_logged_in()) { echo '<button class="logout-btn">'.do_shortcode( "[mepr-logout-link]" ).'</button>'; } else { echo '<button type="button" class="show-btn">LOG IN</button>'; }; ?> Went here to get there: stackoverflow.com/questions/18252433/… Thanks again, @RiddleMeThis Jun 19, 2019 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.