WordPress Shortcode To Show Or Hide Content

Posted · 14 Comments

There are many reasons to show or hide content based on whether someone is to logged in or just visiting your self-hosted WordPress website. People who are registered users on your website shouldn’t be seeing things that only pertain to visitors. There is also content that you only want logged in users to see.

show-hide-content

One method would be to use conditional logic on your menu so that they get menu links to completely different pages based on their logged in status. The If Menu plugin works great for that.

But, what if you don’t want to change pages? What if you want different content to show on the same page? In that case, I prefer to create shortcodes to use in the page. Use the following shortcodes on your page after activating them (instructions below).

How to use the Shortcodes

[loggedin]
Content that only logged in users will see.
[/loggedin]

[loggedout]
Content that only visitors or logged out users will see.
[/loggedout]

Preferred Installation Method

WordPress has a “hidden” feature called, Must Use Plugins. To explain it simply, it is a folder titled “mu-plugins” which is in the wp-content folder. Most likely, you will have to create it. Any PHP found in this folder is considered “must use” and is executed automatically.

Save this ZIP file, extract the php file it contains, upload it to your wp-content/mu-plugins folder (create it if necessary), and the short code will be installed automatically. I consider this the preferred method since it isn’t affected when you update your theme.

Alternate Installation Method

Add this to your Functions.php file by going to Appearance/Editor in your WordPress Dashboard:

// Show or hide content based on whether user is logged in or not
// shortcode for logged in users is [loggedin]content[/loggedin]
// shortcode for logged out users us [loggedout]content[/loggedout]
function loggedincheck( $atts, $content = null ) {
     if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
          return $content;
     return '';
     }
}
add_shortcode( 'loggedin', 'loggedincheck' );

function loggedoutcheck( $atts, $content = null ) {
     if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
          return $content;
     return '';
     }
}
add_shortcode( 'loggedout', 'loggedoutcheck' );

 


Update (28 April 2015):

Thanks to a question from TollyMag in the comments below, I’ve borrowed some code from this post in order to add the ability to only show content to logged in users with specific roles.

User roles available by default in WordPress are: administrator, editor, author, contributor, subscriber. Simply add any role to the $allowed_roles array in the function below to grant them access to the content.

// Show or hide content based on whether user is logged in or not
// shortcode for logged in users is [loggedin]content[/loggedin]
// shortcode for logged out users us [loggedout]content[/loggedout]
function loggedincheck( $atts, $content = null ) {
    $user = wp_get_current_user();
    $allowed_roles = array('administrator', 'editor', 'author');

    if ( is_user_logged_in() && !is_null( $content ) && !is_feed() && array_intersect($allowed_roles, $user->roles ) ) {
          return $content;
          return '';
     }
}
add_shortcode( 'loggedin', 'loggedincheck' );

function loggedoutcheck( $atts, $content = null ) {
    if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
          return $content;
    return '';
    }
}
add_shortcode( 'loggedout', 'loggedoutcheck' );
14 Responses to "WordPress Shortcode To Show Or Hide Content"
  1. Bilal says:

    Hello Jeremy,
    Thank you for the snippet.

    Please correct the pasted code in the end, the sign “&” was coded to &
    I used the code directly without noticing and I got a php error of an unexpected “;” in the code

  2. TollyMag says:

    Hi Jeremy, how can I go about to hide certain content (category with its articles) for anyone below the contributor role?

    • Jeremy Caris says:

      TollyMag, great question. I borrowed a bit of code from here and used it to update my shortcode function to show content only to users that are logged in and have a minimum role of author. Core roles available in WordPress are: administrator, editor, author, contributor, subscriber. Simply add contributor into the $allowed_roles array to grant access to users with that role.

      function loggedincheck( $atts, $content = null ) {
          $user = wp_get_current_user();
          $allowed_roles = array('administrator', 'editor', 'author');
          if ( is_user_logged_in() && !is_null( $content ) && !is_feed() && array_intersect($allowed_roles, $user->roles ) ) {
                return $content;
                return '';
           }
      }
      add_shortcode( 'loggedin', 'loggedincheck' );
      
  3. Jeff Walker says:

    Oh man I love this. I will definitely plan on reserving our Clearance section on our webstore for logged in users only. That makes way too much sense. Thanks for this, Jeremy!

  4. Julio says:

    Hi there,

    this was very usefull, but it doesn’t work when in the “logged in” content you include another shortcode, the page just displays the shortcode itself without processing it, is it possible to add some change to these snippets so other shortcodes works within the [loggedin][/loggeding]?

    Thanks and regards,

    Julio

    • Jeremy Caris says:

      Julio, yes you can update the code to allow it to execute shortcodes within the loggedin shortcode. To do this, make this change within the function/s:

      // Change this line which displays the content
      return $content;
      
      // to this line which displays the content and outputs the shortcodes within the content
      return do_shortcode($content);
      
  5. Hey Jeremy,
    First, thank you for giving beautiful code without any cost. I’m using your ‘alternate installation method’.

    Thank you!

  6. Dozza says:

    Jeremy, thanks for providing this neat bit of code. Could you maybe extend it so that the space occupied by the now hidden content is collapsed?

  7. I owe you a cold pint Jeremy! I’ve been looking for this functionality for a while, and your code hit the mark perfectly!

    Thank you, sir!

  8. Todd says:

    Hi Jeremy, thanks for this code. One question though, why the extra “return ”;” in each function? The way they are indented in your original routines, they look like they are supposed to be outside of the if block so the function returns something in either case. But they are within the if block so they never get executed.

  9. Daniel Parrott says:

    Heya,

    I wasn’t able to post a shortcode (for logging in) within the logged out shortcodes, so I changed your snippet to this and it fixed it. Hope that helps! Thanks for a great code!

    function loggedincheck( $atts, $content = null ) {
    if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
    return do_shortcode($content);
    return ”;
    }
    }
    add_shortcode( ‘loggedin’, ‘loggedincheck’ );

    function loggedoutcheck( $atts, $content = null ) {
    if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() ) {
    return do_shortcode($content);
    return ”;
    }
    }
    add_shortcode( ‘loggedout’, ‘loggedoutcheck’ );

Leave a Reply

Your email address will not be published. Required fields are marked *

 
 
Website by Website by 714Web.com 714Web.com