• Hi everyone,

    On one of my website, I’ve created multiple fields on user profile but I didn’t find a functionality to check if a specific value is set in a multicheckbox.

    I did make a search in the code and I’ve find this functionality:

    function wpmem_user_has_meta( $meta, $value = false, $user_id = false ) {
    	global $wpmem;
    	$user_id = ( $user_id ) ? $user_id : get_current_user_id();
    	$has_meta = false;
    	$user_value = get_user_meta( $user_id, $meta, true );
    	if ( $value ) {
    		$has_meta = ( $user_value == $value ) ? true : $has_meta;
    	} else {
    		$has_meta = ( $value ) ? true : $has_meta;
    	}
    	return $has_meta;
    }

    Is it possible to add a new functionality or update this one with this code :

    function wpmem_user_has_meta( $meta, $value = false, $user_id = false ) {
    	global $wpmem;
    	$user_id = ( $user_id ) ? $user_id : get_current_user_id();
    	$has_meta = false;
    	$user_value = get_user_meta( $user_id, $meta, true );
    	if ( $value ) {
    		// New code 
    		$has_meta = strstr( $user_value, $value ) ? true : $has_meta;
    		// Code OotB
    		//$has_meta = ( $user_value == $value ) ? true : $has_meta;
    	} else {
    		$has_meta = ( $value ) ? true : $has_meta;
    	}
    	return $has_meta;
    }

    With this small change, I’m able to validate if someone has a specific meta inside of an multiple checkbox.

    Thanks for your fantastic plug-in & help !

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chad Butler

    (@cbutlerjr)

    Thanks for your suggestion. I understand what you’re trying to get at (I think), and I applaud your effort on suggesting an update to cover it. I do always appreciate feedback, suggestions, and feature requests.

    I will work on including something to cover this and similar field types. However, it probably will not be exactly as you have here. While using strstr() might be successful in your specific situation, it probably would run into problems as a general use utility.

    For example, if you had a field with possible values that were similar, you could get a false positive.

    A general example would be if there were values such as:

    • blue
    • blue_green
    • blue_yellow

    If you were checking to see if the user had selected “blue” and the user had selected “blue_green” but not “blue”, strstr() would return true, even though the user didn’t have the selection.

    The multiple checkbox and multiple select are stored as a separated values (either pipe separated or comma separated) but need to be “exploded” into an array for proper evaluation. But other field types can (and should) be evaluated as a string.

    I’ll definitely work on including some handling for these field types and I’ll try to get it into the 3.3.0 release. While I can’t promise that I’ll get it in for 3.3.0 for certain, I can say that 3.3.0 does add a filter in this function. It has the same name as the function – wpmem_user_has_meta and it passes the $has_meta boolean, the user ID, and the user’s stored value for this meta. So you could also use that filter to write custom validation.

    There’s more information on the 3.3.0 development process here:
    https://rocketgeek.com/blog/wp-members-3-3-0-development-news/

    Plugin Author Chad Butler

    (@cbutlerjr)

    This is what I have tentatively come up with for 3.3.0 so far.

    /**
     * Checks if a user has a given meta value.
     *
     * @since 3.1.8
     * @since 3.3.0 Added wpmem_user_has_meta filter.
     * @since 3.3.0 Added array check for multi-value fields (multicheckbox and multiselect).
     *
     * @global object  $wpmem     WP_Members object.
     * @param  string  $meta      Meta key being checked.
     * @param  string  $value     Value the meta key should have (optional).
     * @param  int     $user_id   ID of the user being checked (optional).
     * @return boolean $has_meta  True if user has the meta value, otherwise false.
     */
    function wpmem_user_has_meta( $meta, $value = false, $user_id = false ) {
    
    	global $wpmem;
    	$user_id    = ( $user_id ) ? $user_id : get_current_user_id();
    	
    	// Get field type.
    	$fields = wpmem_fields();
    	$multi  = ( 'multicheckbox' == $fields[ $meta ]['type'] || 'multiselect' == $fields[ $meta ]['type'] ) ? true : false;
    	
    	// Get meta.
    	$has_meta   = false;
    	$user_value = get_user_meta( $user_id, $meta, true );
    	
    	// Check meta.
    	if ( $value ) {
    		if ( $multi ) {
    			// Check array of values.
    			$user_value = explode( $fields[ $meta ]['delimiter'], $user_value );
    			$has_meta = ( in_array( $value, $user_value ) ) ? true : $has_meta;
    		} else {
    			// Straight comparison.
    			$has_meta = ( $user_value == $value ) ? true : $has_meta;
    		}
    	} else {
    		// Check if the user has any meta value (regardless of actual value).
    		$has_meta = ( $user_value ) ? true : $has_meta;
    	}
    	
    	/**
    	 * Filter the user has meta value.
    	 *
    	 * @since 3.3.0
    	 *
    	 * @param bool   $has_meta
    	 * @param int    $user_id
    	 * @param string $user_value
    	 */
    	return apply_filters( 'wpmem_user_has_meta', $has_meta, $user_id, $user_value );
    }

    This allows you to check a few different things:

    • Checks regular fields (non-multi value like multiselect and multicheckbox) as a straight comparison, checking the actual value.
    • Checks multi-value metas by exploding into an array and checking if the array contains the requested value.
    • If the function is passed false for $value (or $value is not passed), it just checks if the user has any value for that meta (regardless of the value).

    AND… you’d still be able to check the value using your strstr() method by using the wpmem_user_has_meta filter.

    • This reply was modified 5 years, 8 months ago by Chad Butler.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Serach meta in multicheckbox fields’ is closed to new replies.