• Resolved memento-amsterdam

    (@memento-amsterdam)


    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)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode based on userids’ is closed to new replies.