Limit the readibility of you pages and make some of your content available only to registered users

 

shortcode content hide

 

If you want to hide a piece of information to all anonymous users and display it only for logged people, this shortcode does exactly what you need.

The following code must be inserted in the function.php contained in your child theme (**).

 

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 '';
}
 
add_shortcode( 'logged', 'member_check_shortcode' );
 
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}

 

Add shortcodes in your page

In fact we have here two shortcodes executing two different tasks. The first shortcode ‘visitor’ will  display content to not logged in people, and the second shortcode ‘member’ will display content only to logged in users.

 

 

[visitors] the content displayed to all visitors[/visitors]

 

[logged]the content dedicated to logged in members[/logged]

 
 

Once added the above code in your function.php located in your child theme, just place the following anywhere anywhere in your webpage :

Credits to this nice piece of code: http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content 

 

(**) How to Create a Child Theme

A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.

  1. Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
  2. Create style.css
  3. Create functions.php

For more detailed instructions on child themes you can visit the following links:

https://developer.wordpress.org/themes/advanced-topics/child-themes/

https://blog.hubspot.com/website/wordpress-create-child-theme