Sarathlal N

Short code to check user logged in status - WordPress

In some WordPress project, we need to restrict content on the page as per user logged in stataus. If we are customizing template files to display content, we can easily restrict content by checking user logged in status using is_user_logged_in() function.

Same way, if we need to restrict content on content area, we can use below code snippets & short codes.

Display content for visitors only

Code snippet

add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
	 if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
		return $content;
	return '';

Short code

Some content for the people just browsing your site.

Display content for logged in users only

Code 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 '';

Short code

This is members-only content.

Recent Posts

  1. Handle plugin activation hook in multisite & single site - WordPress
  2. Get WordPress SQL query details
  3. Remove edit, view, trash and quick edit links within posts list of admin screen - WordPress
  4. PHP Errors not displaying or not logging - LAMP server - Solved
  5. Display all hooks sequentialy that run on a page - WordPress

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about them, please get in touch.