Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active June 28, 2018 14:09
Show Gist options
  • Save cosmomathieu/f0350dc65cadbc94496abdb8e824bbb4 to your computer and use it in GitHub Desktop.
Save cosmomathieu/f0350dc65cadbc94496abdb8e824bbb4 to your computer and use it in GitHub Desktop.
WordPress shortcodes to show and hide text based on user authentication state
<?php
if ( ! function_exists('show_logged_in_content')) {
/**
* Display text to logged in Donation users only
*
* <code>
* [loggedin]This content will only be displayed to logged in users.[/loggedin]
* </code>
*
* @author Cosmo Mathieu <cosmo@cosmointeractive.co>
* @link http://acroweb.co.uk/user-logged-in-shortcode/
* @param $params
* @param $content
* @return string
*/
function show_logged_in_content ($params, $content = null){
//check tha the user is logged in
if ( is_user_logged_in() ){
//user is logged in so show the content
return $content;
} else {
//user is not logged in so hide the content
return;
}
}
}
//add a shortcode which calls the above function
add_shortcode('loggedin', 'show_logged_in_content' );
if ( ! function_exists('show_logged_out_content')) {
/**
* Display text to logged out Donation users
*
* <code>
* [loggedout]This content will only be displayed to logged out users.[/loggedout]
* </code>
*
* @author Cosmo Mathieu <cosmo@cosmointeractive.co>
* @link https://714web.com/wordpress-shortcode-to-show-or-hide-content/
* @param $params
* @param $content
* @return string
*/
function show_logged_out_content($params, $content = null) {
if ( !is_user_logged_in() && !is_null( $content )) {
return $content;
} else {
return '';
}
}
}
add_shortcode( 'loggedout', 'show_logged_out_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment