Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save briankompanee/3b4a4c751c886ba9855fd44c6b2d905a to your computer and use it in GitHub Desktop.
Save briankompanee/3b4a4c751c886ba9855fd44c6b2d905a to your computer and use it in GitHub Desktop.
WordPress: Add a shortcode to use in the content that shows content to non-logged in users. If user is logged in then they will not see the content.
<?php
/**
* Add A Shortcode allowing content for non-logged in users.
* Add to functions.php
* This could used in a situation where you have gated content and the user needs to be logged in to view.
* An example of usage is: [not_logged_in]You must be logged in to view this content[/not_logged_in]
*/
function check_user ($params, $content = null){
//if the user is not logged in. You can remove the ! if you want to change the statement to is logged in.
if ( !is_user_logged_in() ){
return $content;
}
else{
return;
}
}
add_shortcode('not_logged_in', 'check_user' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment