Display Content based on User Meta Value

Est. Reading: 3 minutes
By: FDM Digital
Created: 11/02/2021
Difficulty: Intermediate

Display Content based on User Meta Value

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:

  • user_id – id of the user which you want to retrieve the meta from. Leave empty if it should be equal to the current user.
  • key – meta key of the field that you want to retrieve
  • wpautop = “on” – this is used for textarea or wysiwyg fields
  • size = “100” – this is used for the avatar field to specify the size of it

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.

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.


Your content if FALSE

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.

Leave a Reply

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
crosschevron-leftchevron-rightarrow-right