Build a Simple User Logged In Shortcode in WordPress

  • November 06, 2013

If you are looking for the best place to host buy cheap xanax your WordPress site, knowing that it will run very fast and always be secure, be sure to check out WPEngine

A Simple User Logged in Shortcode in WordPress

Open up your functions.php file in your theme and add the following code:

function check_user ($params, $content = null){
  //check tha the user is logged in
  if ( is_user_logged_in() ){

    //user is logged in so show the content
    return $content;

  }

  else{

    //user is not logged in so hide the content
    return;

  }

}

//add a shortcode which calls the above function
add_shortcode('loggedin', 'check_user' );

And there you have it, you can then write something like:

[loggedin]This content will only be displayed to logged in users.[/loggedin]

…and it should work.

Share This

Emile

About Emile

Emile is the director of Acroweb. He is a Wordpress and Drupal web developer and project manager with an extensive knowledge of web strategies and SEO. He lives on the South Coast UK with his wife and daughter and spends a lot of his time chasing waves and wind around the country.

  • Mike Levine

    Thanks! This is awesome… I always prefer a well-researched DIY solution to adding another weighty plugin to the mix. : )

  • Sergio Ronei Hentz

    Hello Emile!!

    Congratulations about shortcode ! It’s wonderfull and works fine!

    I wonder if there how to make a shortcode to display something when the user is NOT logged.

    Thanks and sorry thath bad english 😉

    • emilekott

      Hi Sergio,

      I think the best way would be to rebuild the shortcode so that it takes an “attribute”, giving something like:

      [logged_in user=”yes”]Display this only to logged in users[/logged_in]

      [logged_in user=”no”]Display this only to non-logged in users[/logged_in]

      I am a bit short of time today but will try to add this to the solution later as it will make it more useful.

      In the mean time, the WordPress API documentation ( http://codex.wordpress.org/Shortcode_API ) has a lot of useful information.

Have a project that you want to talk about? Get A Quote