Skip Navigation

[Gelöst] Check if user is logged in

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
How can I check if the current user is logged in and present different content depedning on this?

Solution:
You can use HTML conditional.
The example code is:

[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'true' )"]I am Logged in[/wpv-conditional]
 
[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'false' )"]I am not Logged in[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created vor 7 Jahre, 8 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by julieP vor 7 Jahre, 8 Monate.

Assisted by: Beda.

Author
Artikel
#420820

I am trying to: follow the CRED training course on this page https://toolset.com/documentation/user-guides/cred-training-course/part-7-building-account-page-login-form/

I'm using Layouts plugin.

I expected to see: the results as set out under "Separating between visitors and logged-in users"

Instead, I got:

When using a Visual Editor Cell Type and conditional output coding like this as detailed on the training page:

[wpv-conditional if="( '[wpv-current-user info='logged-in']' eq 'false' )"]I'm a Visitor[/wpv-conditional]<br />
[wpv-conditional if="( '[wpv-current-user info='logged-in']' eq 'true' )"]I'm Logged In[/wpv-conditional]

the page is empty for both logged-in and not logged-in users.

When using a Visual Editor Cell Type and conditional output coding like this:-

[wpv-conditional if="( '[wpv-current-user]' eq 'logged-in' )" evaluate="false"]I'm A Visitor[/wpv-conditional]<br />
[wpv-conditional if="( '[wpv-current-user]' eq 'logged-in' )" evaluate="true"]I'm Logged In[/wpv-conditional]

Both logged-in and not logged-in users see "I'm A Visitor"

If I use a Content Template cell instead, the same results occur but the text appears slightly lower down the screen (as if an empty line is returned for the option "I'm Logged In".

Any ideas please?

Thanks

#420925

That ShortCode option is not "logged-in" or "logged-out" but "logged_in" or "logged_out".

Actually, if you set the condition with the GUI, this will be automatically correct.

The correct Code is:

[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'true' )"]I am Logged in[/wpv-conditional]

[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'false' )"]I am not Logged in[/wpv-conditional]

This is also exactly how Agnes explains it in the tutorial.
Let me know if there are any typos there.

#420972

Aha I see; I used dashes instead of underscores! Thanks for spotting that.

Agnes' code works but if I use the GUI to set the conditions, it produces this which is slightly different to Agnes' code :-

[wpv-conditional if="( '[wpv-current-user]' eq 'logged_in' )" evaluate="false"]I'm A Visitor[/wpv-conditional]<br />
[wpv-conditional if="( '[wpv-current-user]' eq 'logged_in' )" evaluate="true"]I'm Logged In[/wpv-conditional]

and produces the right output if not logged in but the wrong one if logged in.

I'm happy to use Agnes' code but curious as to why the code generated using the GUI doesn't work correctly.

#421079

See what the steps Agens indicate:
Separating between visitors and logged-in users > step 5.

You have to edit this ShortCode.

Much simpler is to highlight it (the [wpv-current-user] part) and then click on Fields and Views.
Find the "Current user" and click on it.

You will see major options appear.
One of them is Logged in.
Check the radio and insert the ShortCode.

Now it should look like in the Tutrial and also work.

In future the Conditional Display GUI will support options of ShortCodes but not yet.
Right now you need to edit it manually or use the "Fields and Views" GUI.

#421724

OK Thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.