How To Build A WordPress User Only Shortcode

Looking to build an exclusive logged-in user-only shortcode? All you may need is to copy this function into your WordPress theme’s funcitons.php file.

Register the exclusive content shortcode

To register the exclusive content shortcode on your WordPress website, simply copy and paste in the code below into your WordPress theme’s functions.php file.

function custom_members_only_shortcode( $atts, $content ) {
     if ( is_user_logged_in() ) {
          return $content;
     } else {
          $output = '<div>';
          $output .= '<p>Exclusive content, you must be a registered user to view this.</p>';
          $output .= '</div>';
          return $output;
     }
}

add_shortcode( 'exclusive', 'custom_members_only_shortcode' );

Shortcode usage

To use this shortcode, simply wrap your secure content in the [exclusive][/exclusive] shortcode.

Get the Exclusive Content Shortcode plugin

We’ve made it easier for you! You can use this shortcode by getting our plugin hosted in the official WordPress plugin repository.

Integrating with the Paid Memberships Pro plugin

To integrate the above solution with the Paid Memberships Pro plugin, simply change the if ( is_user_logged_in() ) { line with the below.

if ( is_user_logged_in() && pmpro_hasMembershipLevel( 1 ) ) {

Remember to update the 1 to the restricted membership level inside the pmpro_hasMembershipLevel() function.