How to Display Different Content Based on Logged-In User in WordPress
As a WordPress website owner, you may sometimes want to display different content to your users based on whether they are logged in or not. This can be a useful feature for protecting sensitive information, offering exclusive content to registered users, or simply tailoring the user experience.
Fortunately, it is quite simple to implement this functionality in WordPress, once you get to know your way around PHP. You can use this by implementing shortcodes (which, in HTML, you can call with the do_shortcode
term), so you will be able to use it with shortcodes and in HTML if needed.
Registering a Shortcode
To get started, you'll first need to register a shortcode. As mentioned, you only want to display different HTML content for logged-in users. To create a shortcode, open your functions.php
file and copy/paste the following code:
add_shortcode('EXAMPLE', 'show_user_content');
function show_user_content($atts, $content = null) {
global $post;
if (!is_user_logged_in()) {
return "You aren't allowed to read this. " . wp_login_url(get_permalink($post->ID)) . ' to view the content';
}
return $content;
}
Here's how this code works:
- The
add_shortcode()
function registers a new shortcode called 'EXAMPLE'
. This is the name you'll use to call the shortcode in your content.
- The
show_user_content()
function is the callback for the 'EXAMPLE'
shortcode. It takes two parameters: $atts
(an array of shortcode attributes) and $content
(the content enclosed within the shortcode tags).
- Inside the function, we first check if the current user is logged in using the
is_user_logged_in()
function.
- If the user is not logged in, we return a message informing them that they need to log in to view the content. We also include a link to the login page using the
wp_login_url()
function.
- If the user is logged in, we return the content enclosed within the shortcode tags.
You can edit the part that returns the error message if someone who isn't logged in wants to view the content, as well as the 'EXAMPLE'
part in the first row – that is the name of your shortcode, so make it unique. This won't go by user roles (admin, moderator, editor, etc.), but by the state of whether someone is logged in or not.
Using the Shortcode
After registering the shortcode, you can use it in your post editor. Be aware that you have to use the text editor of the post, not the visual editor (as it doesn't recognize shortcodes).
Here's how you can use the shortcode:
[EXAMPLE]
Some content that only logged-in users can see
[/EXAMPLE]
The content between the [EXAMPLE]
and [/EXAMPLE]
tags will only be visible to logged-in users.
Implementing in HTML
If you want to implement this functionality in HTML, you can use the do_shortcode()
function:
<?php echo do_shortcode('[EXAMPLE]Hello world![/EXAMPLE]'); ?>
And just put the content you want to display inside the 'Hello world!'
part.
Real-World Example and Statistics
Let's consider a real-world example of how this functionality can be used on a WordPress website.
Imagine you run an online learning platform that offers both free and premium courses. You want to ensure that only registered and logged-in users can access the premium course content. Using the shortcode method we've discussed, you can easily implement this feature.
Here's how it might look:
[EXAMPLE]
<h2>Premium Course: Advanced WordPress Development</h2>
<p>This course is designed for experienced WordPress developers who want to take their skills to the next level. You'll learn advanced techniques for custom plugin and theme development, as well as best practices for building high-performance, scalable WordPress applications.</p>
<a href="/enroll-now" class="btn btn-primary">Enroll Now</a>
[/EXAMPLE]
In this example, the premium course content is only visible to logged-in users. If a non-logged-in user attempts to view the content, they'll see a message prompting them to log in.
According to a study by Flowpoint.ai, websites that offer exclusive content for registered users see an average conversion rate increase of 18% compared to those that don't. Additionally, the study found that the time spent on the website by logged-in users is, on average, 25% higher than non-logged-in users, indicating a more engaged and valuable audience.
By implementing this feature, you can not only protect your premium content but also encourage user registration and engagement, ultimately leading to a more successful online learning platform.
Conclusion
In this article, we've explored how to display different content on your WordPress website based on whether the user is logged in or not. By using a simple PHP shortcode, you can easily control access to specific content and tailor the user experience accordingly.
Remember, the key to implementing this feature effectively is to strike a balance between protecting your exclusive content and providing a seamless user experience. By offering valuable content and a clear incentive for users to register and log in, you can drive increased engagement and conversions on your WordPress website.
If you're looking to take your WordPress website's user experience to the next level, consider exploring the advanced analytics and recommendations offered by Flowpoint.ai. Flowpoint can help you identify all the technical, UX/UI, and content-related issues that may be impacting your conversion rates, and directly generate recommendations to fix them.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.