November 2013 - ACROWEB

Build a Simple User Logged In Shortcode in WordPress

Posted by | Basic Coding, WordPress | 3 Comments

Sometimes it is useful to display further content only to logged in users. A very simple way of doing this is to build a user logged in shortcode.

This means that when you are editing your content, you can add something like:

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

There are plugins that can offer this kind of thing but you can add it to your theme yourself very easily.

If you are looking for the best place to host 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

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