How To Create Public and Private ShortCodes in WordPress
erics, October 22nd, 2012
Add this to your theme’s functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* Add the [private] shortcode. */ add_shortcode( 'private', 'is_user_logged_in_shortcode' ); /* Add the [public] shortcode. */ add_shortcode( 'public', 'is_user_not_logged_in_shortcode' ); function is_user_logged_in_shortcode( $attr, $content = null ) { /* If it is a feed or the user is not logged in, return nothing. */ if ( is_feed() || !is_user_logged_in() || is_null( $content ) ) return ''; /* Return the content. */ return do_shortcode( $content ); } function is_user_not_logged_in_shortcode( $attr, $content = null ) { /* If it is a feed or the user is logged in, return nothing. */ if ( is_feed() || is_user_logged_in() || is_null( $content ) ) return ''; /* Return the content. */ return do_shortcode( $content ); } |