0

I want to check at page load if the user is logged in or not and hide the login mask I programmed accordingly. There is a reason I'm using said login mask instead of redirecting to the WordPress standard login.

The idea was to use functions.php:

if ( is_user_logged_in() ) {
    echo '<li id="text-2" style="display:none;">';
} else {
    echo '<li id="text-2">'; }

This code works, but my page kind of stops loading after that. And if I pack the entire thing into a function I get an error - Parse error has been resolved by Answer01 by Brad Dalton.

Long story short: How do I show/hide certain elements depending on whether a user is logged in or not?

edit01: Here's the parts before the code I posted above:

<?php

function modify_user_contact_methods($user_contact) {
    // Add new fields
    $user_contact['uidnumber'] = 'UID-Nummer';
    return $user_contact;
}

add_filter('user_contactmethods', 'modify_user_contact_methods');

?>
<?php
if ( is_user_logged_in() ) {
    echo '<li id="text-2" style="display:none;">';
} else {
    echo '<li id="text-2">';
 }
?>

edit02: I've tried using the code provided below with the action_hook:

add_action( 'loop_start', 'loginCheck' );
function loginCheck() {
if ( is_user_logged_in() ) {
    echo '<li id="text-2" class="hidden">';
} else {
    echo '<li id="text-2">';
 }
}

Yet now the page looks like this: - solved in edit03

edit03: I've used a temporary work-around by switching loop_start with loop_end. However as I said earlier, the section I want to hide is still being displayed.

edit04: WP_DEBUG.. let's see.. - no relevant finding related to the problem

edit05: So here's the summary of the current situation:

  • Page is loading, all elements are displayed, check.
  • jQuery opens and closes the Login-Box if the user is not logged in, check.
  • Content is hidden when user is logged in to prevent confusion, UNSOLVED.

So essentially it's only the function that doesn't do what it's supposed to.

Here's a link to the website: easy2work.at

What I want is to hide the "Firmen-Login | Anmelden" once the user is logged in and display something else up there which is ready to be implemented.

  • 1
    Read your syntax error message, there is some syntax missing before your funtion call, something like a comma, semi-colon, bracket, full stop etc. – Pieter Goosen Jul 27 '15 at 8:50
  • I tried using add_action('init', loginCheck); already. same error. – theHubi Jul 27 '15 at 9:00
  • 1
    Without seeing the code, it is impossible to say what exactly is causing the error. Post the effected section of code in an edit please. Do not post code in comments as it is unreadable – Pieter Goosen Jul 27 '15 at 9:00
  • enable WP_DEBUG to true to see what's the inner problem resides there after. – Mayeenul Islam Jul 27 '15 at 9:31
  • 1
    The edit is much better. Please ask one specific question at a time, that is policy and will also not cause confusion – Pieter Goosen Jul 27 '15 at 10:36
2

Your code won't work in a themes functions file unless it includes a hook.

Try something like:

add_action( 'loop_start', 'your_function' );
function your_function() {

if ( is_user_logged_in() ) {
    echo '<li id="text-2" class="hide">';
} else {
    echo '<li id="text-2">'; 
}}
  • So the site is loading again, yet nothing changed concerning my mentioned problem. Also the site stops loading after the header (see above) – theHubi Jul 27 '15 at 9:12
  • So here's the current situation: Site is loading, jQuery does what it's supposed to. Only this function does not want to work. I'll add edit05 and try to be as exact as possible. – theHubi Jul 27 '15 at 10:05

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.