Sometimes you want to hide certain information on your site unless the visitor is logged in. Maybe you have “member only” content, or perhaps you want to personalize the visitor’s experience on your site (“Hello _____”). Whatever the reason it is easy to hide content for logged out visitors.
This code snippet will create a shortcode [hide_me]
that you can use in your WordPress pages, posts, Custom Post Types, widgets, or anywhere shortcodes can be run. Copy and paste it into your theme’s functions.php file or in a custom plugin.
1 2 3 4 5 6 7 8 | function sd_hidden_content( $atts, $content = null ) { if ( !is_user_logged_in() ) { return false; } else { return $content; } } add_shortcode( 'hide_me', 'sd_hidden_content' ); |
You would use this shortcode on a page like this:
This is some content that should be visible to everyone. [hide_me]This is some content that should only be visible to logged in visitors.[/hide_me] |
The shortcode can be used multiple times on a page, so it’s easy to hide different content blocks that may be separated by “public” content throughout the page just by repeating the same process shown above.
This is some content that should be visible to everyone. [hide_me]This is some content that should only be visible to logged in visitors.[/hide_me] This is some more content that should be visible to everyone. [hide_me]This is some more content that should only be visible to logged in visitors.[/hide_me] |
You can change the hide_me shortcode to whatever you want, by editing the hide_me portion of the code on line 8 above.
Leave a Reply