Short code to check user logged in status - WordPress
In some WordPress project, we need to restrict content on the page as per user logged in stataus. If we are customizing template files to display content, we can easily restrict content by checking user logged in status using is_user_logged_in()
function.
Same way, if we need to restrict content on content area, we can use below code snippets & short codes.
Display content for visitors only
Code snippet
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
Short code
[visitor]
Some content for the people just browsing your site.
[/visitor]
Display content for logged in users only
Code snippet
add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
Short code
[member]
This is members-only content.
[/member]
Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!
Recent Posts
- REST API Methods Explained with Best Practices for Building Clean and Secure APIs
- My 28-Day Plan to Master Modern WordPress Development Using AI Tools
- Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
- WordPress Transients Explained - A Developer's Guide to Site Performance
- Behind the Click - The Hidden Journey of Your Web Requests
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.