functions - If user is logged-in displayhide something

admin2025-06-02  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.

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.

Share Improve this question edited Jul 27, 2015 at 10:25 theHubi asked Jul 27, 2015 at 8:45 theHubitheHubi 892 gold badges4 silver badges14 bronze badges 6
  • 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 Commented Jul 27, 2015 at 8:50
  • I tried using add_action('init', loginCheck); already. same error. – theHubi Commented Jul 27, 2015 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 Commented Jul 27, 2015 at 9:00
  • enable WP_DEBUG to true to see what's the inner problem resides there after. – Mayeenul Islam Commented Jul 27, 2015 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 Commented Jul 27, 2015 at 10:36
 |  Show 1 more comment

1 Answer 1

Reset to default 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">'; 
}}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748822204a314011.html

最新回复(0)