Advertisement
geminilabs

[site-reviews] shortcodes to restrict user content on page

Jun 26th, 2019 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. //
  2. // Alternative is to use: https://wordpress.org/plugins/user-access-shortcodes/
  3. //
  4.  
  5. /**
  6.  * Registers the [is_admin][/is_admin] shortcode
  7.  * This shortcode displays content only to administrator users
  8.  *
  9.  * Example usage: [is_admin][site_reviews_form][/is_admin]
  10.  *
  11.  * Paste this in your active theme's functions.php file
  12.  * @param array $atts
  13.  * @param string $content
  14.  * @return string
  15.  */
  16. add_shortcode('is_admin', function ($atts, $content = '') {
  17.     return (current_user_can('administrator') && !empty($content) && !is_feed())
  18.         ? do_shortcode($content)
  19.         : '';
  20. }, 10, 2);
  21.  
  22. /**
  23.  * Registers the [is_member][/is_member] shortcode
  24.  * This shortcode displays content only to logged in users
  25.  *
  26.  * Example usage: [is_member][site_reviews_form][/is_member]
  27.  *
  28.  * Paste this in your active theme's functions.php file
  29.  * @param array $atts
  30.  * @param string $content
  31.  * @return string
  32.  */
  33. add_shortcode('is_member', function ($atts, $content = '') {
  34.     return (is_user_logged_in() && !empty($content) && !is_feed())
  35.         ? do_shortcode($content)
  36.         : '';
  37. }, 10, 2);
  38.  
  39. /**
  40.  * Registers the [is_guest][/is_guest] shortcode
  41.  * This shortcode displays content only to users who are not logged in
  42.  *
  43.  * Example usage: [is_guest][site_reviews_form][/is_guest]
  44.  *
  45.  * Paste this in your active theme's functions.php file
  46.  * @param array $atts
  47.  * @param string $content
  48.  * @return string
  49.  */
  50. add_shortcode('is_guest', function( $atts, $content = '') {
  51.     return ((!is_user_logged_in() && !empty($content)) || is_feed())
  52.         ? do_shortcode($content)
  53.         : '';
  54. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement