geminilabs

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

Jun 26th, 2019 (edited)
381
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Registers the [is_admin][/is_admin] shortcode
  3.  * This shortcode displays content only to administrator users
  4.  *
  5.  * Example usage: [is_admin][site_reviews_form][/is_admin]
  6.  *
  7.  * Paste this in your active theme's functions.php file
  8.  * @param array $atts
  9.  * @param string $content
  10.  * @return string
  11.  */
  12. add_shortcode('is_admin', function ($atts, $content = '') {
  13.     return (current_user_can('administrator') && !empty($content) && !is_feed())
  14.         ? do_shortcode($content)
  15.         : '';
  16. }, 10, 2);
  17.  
  18. /**
  19.  * Registers the [is_member][/is_member] shortcode
  20.  * This shortcode displays content only to logged in users
  21.  *
  22.  * Example usage: [is_member][site_reviews_form][/is_member]
  23.  *
  24.  * Paste this in your active theme's functions.php file
  25.  * @param array $atts
  26.  * @param string $content
  27.  * @return string
  28.  */
  29. add_shortcode('is_member', function ($atts, $content = '') {
  30.     return (is_user_logged_in() && !empty($content) && !is_feed())
  31.         ? do_shortcode($content)
  32.         : '';
  33. }, 10, 2);
  34.  
  35. /**
  36.  * Registers the [is_visitor][/is_visitor] shortcode
  37.  * This shortcode displays content only to users who are not logged in
  38.  *
  39.  * Example usage: [is_visitor][site_reviews_form][/is_visitor]
  40.  *
  41.  * Paste this in your active theme's functions.php file
  42.  * @param array $atts
  43.  * @param string $content
  44.  * @return string
  45.  */
  46. add_shortcode('is_visitor', function( $atts, $content = '') {
  47.     return ((!is_user_logged_in() && !empty($content)) || is_feed())
  48.         ? do_shortcode($content)
  49.         : '';
  50. }, 10, 2);
RAW Paste Data Copied