Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active September 20, 2017 23:32
Show Gist options
  • Save wpscholar/1242539 to your computer and use it in GitHub Desktop.
Save wpscholar/1242539 to your computer and use it in GitHub Desktop.
WordPress shortcode will only display content to registered users.
<?php
/**
* WordPress shortcode only displays content to registered users. Could be improved by
* passing role as an attribute to only allow certain types of users.
*
* Example: [member]This text will be only displayed to registered users.[/member]
*/
function members_only_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'member', 'members_only_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment