Tag: User Meta

Recently a question was asked in the Formidable Slack group asking if there was a way to show / hide content in a page or view based on the value of a users user-meta table.

Formidable offers a number of ways to pre-populate form fields with user-meta data, and also create your own custom user-meta fields with the Registration add-on, but the built-in conditional shortcodes didn't quite do what was required.

We found this requirement interesting and worked on a solution that used as much of the built-in functionality of Formidable as possible.

Step 1: Add the Check Parameter Value Shortcode

The first step is to add the 'Check Parameter Value Shortcode' to your site using the PHP snippet HERE.

The easiest way to do this is using the Code Snippets plugin or you can add it to your functions.php file or a custom plugin.

Step 2: Add a Custom User Meta Shortcode

This step is optional as it's not required if you only want to use this functionality in a view however if you plan on using this functionality in posts or pages then this is required.

Add this snippet (credit to CozmosLabs for this) to a new Code Snippet or your functions.php file:

add_shortcode('user_meta_value', 'user_meta_shortcode_handler');

function user_meta_shortcode_handler($atts, $content = null) {

	if (!isset($atts['user_id'])) {
		$user = wp_get_current_user();
		$atts['user_id'] = $user - > ID;
	}
	if (!isset($atts['size'])) {
		$atts['size'] = '50';
	}
	if (!isset($atts['post'])) {
		$atts['post'] = '';
	}
	if (!isset($atts['wpautop'])) {
		$atts['wpautop'] = '';
	}

	$user = new WP_User($atts['user_id']);

	if (!$user - > exists()) return;

	if ($atts['key'] == 'avatar') {
		return $atts['pre'].get_avatar($user - > ID, $atts['size']).$atts['post'];
	}
	if ($user - > has_prop($atts['key'])) {
		if ($atts['wpautop'] == 'on') {
			$value = wpautop($user - > get($atts['key']));
		} else {
			$value = $user - > get($atts['key']);
		}
	}

	if (!empty($value)) {
		return $atts['pre'].$value.$atts['post'];
	}

	return;
}

This will create a new shortcode that you can use anywhere on your site for displaying User Meta values.

Shortcode: [user_meta_value key="whatever"]

Replace 'whatever' with the user meta key and the shortcode will display the matching value.

The shortcode has some optional paramaters that are NOT required for it's basic usage but may be useful.

Parameters:

Note: The snippet above is slightly different to the original version on the CozmosLabs website. The original code creates a shortcode called user_meta which you may notice is the same as the existing shortcode in Formidable when used as a default value. If you plan on using this only within a view you can use the existing shortcode and you don't need this code at all however if you do want to use it in pages or posts then the above version is required which creates a shortcode called user_meta_value instead so there's no conflict.

Step 3: Putting it all together

Using these additional features we can now create the required functionality.

To start with we're going to make use of the frm-set-get shortcode to store the user-meta value (retrived by our new shortcode) as a parameter.

[frm-set-get param=user_business][user_meta_value key=Client_Business_Name][/frm-set-get]

You can read more about the frm-set-get shortcode HERE but essentially whatever you put inside the shortcode will be stored as a parameter value. We're going to put our new user_meta_value shortcode inside to use later.

Once we have that set-up we can then use the Check Parameter Value Shortcode to create our conditional content.

E.g.

[if_get_param name="user_business" type="equals" value="Acme Co."]Your content if TRUE[/if_get_param]
[if_get_param name="user_business" type="not_equal" value="Acme Co."]Your content if FALSE[/if_get_param]

The above if_get_param shortcodes will check the parameter 'user_business' for the value 'Acme Co.' and then display a message if true or a different message if false.

This is just a basic example and you can obviously adjust it to whatever your requirements are.

And thats really about it.

×Warning: This tutorial was created 2043 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

Add this snippet to your functions.php file or use the Code Snippets plugin:

add_shortcode('users-email-domain',extract_domain_func);
function extract_domain_func(){
   $current_user = wp_get_current_user();
   $email = $current_user->user_email;
   if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
      // split on @ and return last value of array (the domain)
     $domain = array_pop(explode('@', $email));
     return $domain;
   }
}

Usage [users-email-domain]

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right