Home / functions.php / Member only content
  • Member only content

    Adding this PHP code to the functions.php file of your wordpress theme will allow you to place content you only wish members to see within the members short code [member][/member].

    wordpress snippet : 

    <>
    add_shortcode( 'member', 'member_check_shortcode' );
    
    function member_check_shortcode( $atts, $content = null ) {
    	 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
    		return $content;
    	return '';
    }
    

    wordpress snippet : 

    <>
    [member]
    This text will be only displayed to registered users.
    [/member]
    

    ( WordPress codex functions, hooks, in this snippet. )

    register, is_user_logged_in, add_shortcode, _n, is_feed,
  • EarthShaver

    Sweet Snippet!!
    but leaving the space empty for non-member visitors would be too boring. how about inserting some text-message for non-member visitors, like “You must be logged in to view this content” or something like that?

    Oh, and thanks for the good work!!

    • http://wpsnipp.com Kevin Chard

      just return something other within return ‘some text'; and not blank should do the trick.

Build a better with wpsnipp.com

Weekly snippets, themes, deals coupons roundup.

x No Thanks