Title: Shortcode based on userids
Last modified: June 8, 2021

---

# Shortcode based on userids

 *  Resolved [memento-amsterdam](https://wordpress.org/support/users/memento-amsterdam/)
 * (@memento-amsterdam)
 * [5 years ago](https://wordpress.org/support/topic/shortcode-based-on-userids/)
 * We needed an extension of the shortcode with the ability to restrict based on
   user ID’s, here what we changed:
 * In /classes/Shortcodes.php we added an attribute ‘userids’ which we want to fill
   with a list of ID’s:
 * `'userids' => array(),`
 * Added to $atts…
 * Then in classes/Is.php we extended the accessible function with the new attribute:
 *     ```
       public static function accessible( $who = '', $roles = array(), $userids = array(), $args = array() ) {
   
       if ( ! is_array( $userids ) ) {
           $userids = array();
       }
       ```
   
 * Then in the $who switch we changed this case:
 *     ```
       case 'logged_in':
       				if ( ! $logged_in ) {
       					$exclude = true;
       				} elseif ( ! empty( $roles ) || ! empty ($userids) ) {
   
       				  if(empty ($userids)){
   
       					// Checks all roles, should not exclude if any are active.
       					$valid_role = false;
   
       					foreach ( $roles as $role ) {
       						if ( current_user_can( $role ) ) {
       							$valid_role = true;
       							break;
       						}
       					}
   
       				  }else{
   
         					// Checks all roles, should not exclude if any are active.
         					$valid_role = false;
       					$cuid = get_current_user_id();
   
         					foreach ( $userids as $userid ) {
         						if ( $userid == $cuid ) {
         							$valid_role = true;
         							break;
         						}
         					}
   
       				  }
   
       					if ( ! $valid_role ) {
       						$exclude = true;
       					}
       				}
       				break;
       ```
   
 * Feel free to use this (or add it to the Plugin)!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Daniel Iser](https://wordpress.org/support/users/danieliser/)
 * (@danieliser)
 * [4 years, 12 months ago](https://wordpress.org/support/topic/shortcode-based-on-userids/#post-14560836)
 * [@memento-amsterdam](https://wordpress.org/support/users/memento-amsterdam/) –
   Thanks for sharing, would you be interested in submitting a pull request?
 *  Thread Starter [memento-amsterdam](https://wordpress.org/support/users/memento-amsterdam/)
 * (@memento-amsterdam)
 * [4 years, 12 months ago](https://wordpress.org/support/topic/shortcode-based-on-userids/#post-14561290)
 * Hi Daniel, sure! Just did a pull request on Github.
 *  Plugin Author [Daniel Iser](https://wordpress.org/support/users/danieliser/)
 * (@danieliser)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/shortcode-based-on-userids/#post-14633302)
 * [@memento-amsterdam](https://wordpress.org/support/users/memento-amsterdam/) –
   Awesome, sorry for the delay, but will review it this week and get a patch out
   with attribution 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Shortcode based on userids’ is closed to new replies.

 * ![](https://ps.w.org/content-control/assets/icon-256x256.gif?rev=2968535)
 * [Content Control - The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More](https://wordpress.org/plugins/content-control/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/content-control/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/content-control/)
 * [Active Topics](https://wordpress.org/support/plugin/content-control/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/content-control/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/content-control/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Daniel Iser](https://wordpress.org/support/users/danieliser/)
 * Last activity: [4 years, 11 months ago](https://wordpress.org/support/topic/shortcode-based-on-userids/#post-14633302)
 * Status: resolved