Tag: Shortcode

This is not very elegant and could easily be expanded, but while it was fresh in my head, I thought I would share it in case it helps anyone.

The shortcode asks for two attributes: name and values

If the param value is contained in the list, it will hide any content you've put in between the opening and closing shortcodes.

E.g.

Your View: [check_param_list name="project" values="Project A, Project B, Project C"] Here's the content I want to show if the project is NOT listed[/check_param_list]

Scenario 1 - Your project param = Project A: Nothing will show

Scenario 2 - Your project param = Project D: "Here's the content I want to show if the project is NOT listed" will be displayed

This was my quick and dirty way of handling a situation where I have a standard view for most projects using a central form, but there are a handful of projects that have custom views. In this situation, I don't want to have lots of separate pages for each one. By using this approach, I can have a single page with this shortcode (which has the standard view as its content), followed by two (for now) frm-condition shortcodes, one for each of the projects that have custom views. If I need to add an additional custom view I can add the new project to the list of values and include an additional frm-condition shortcode to display the custom view.*

While being a bit clunky, it has the advantage of being able to be edited/added to by colleagues who are unable to edit shortcodes directly.

Here's the PHP for the shortcode, which can be added to theme functions or via the code snippets plugin.

add_shortcode('check_param_list', 'check_param_value_against_multiple_values');
function check_param_value_against_multiple_values( $atts, $content="" ) {
if ( ! isset( $atts['name'] ) || ! isset( $atts['values'] ) ) {
return $content;
}
$get_param_name = isset( $_GET[ $atts['name'] ] ) ? $_GET[ $atts['name'] ] : '';
$get_param_name = utf8_decode( urldecode( $get_param_name ) );
$values_array = explode(",",$atts['values']);
if (in_array($get_param_name, $values_array)) {
$content = "";
}
return do_shortcode( $content );
}

 

NOTE: The following is really not specific to Formidable, rather it's specific to WordPress, however, there are so many opportunities to use this in Formidable that I've written it as if it were a "Formidable thing".  Amusingly enough, when I got done with this, I discovered this entry in the Formidable Knowledgebase.

/* add_shortcode is a WordPress function and its first and second parameters can be anything you want, though you should make sure both are as unique as possible to avoid conflicts.  In the below example, I added 'ff_custom_' to minimize the chances of a conflict.  The second parameter must match exactly with the name of the function attached to the shortcode  */

add_shortcode('myCustomShortcode', 'ff_custom_doSomething');

/* In your PHP function, you'll do all the calculations, database searching or whatever and then use RETURN to send the information back to Formidable (or WordPress or whatever).  The data you return can be as simple as true/false or as "complex" as an entire stream of HTML.  Actually, you don't have to pass back any information (so just use return;) if you want the shortcode to do something rather than return something.

The parameter $atts is an array of attributes (parameters) you can pass to the function when calling it from a Formidable page or view, e.g., [myCustomShortcode entry=[id]].  In that example, there's a parameter named "entry" and its value is the value Formidable assigns to [id].

In this function, you can do whatever you can accomplish in PHP.  Frequently you pass in a Formidable entry id or key and then use the Formidable functions to get the information from that entry and do something with it.  That's what I'll do for the example below in which we return the square root of a number since we can't easily do square root using Formidable's math functions.  See this Formidable example.

*/

function ff_custom_doSomething($atts) {

  $entry_id = $atts["entry"];

  $number_field_id = 101; // example field id

  $val = FrmEntryMeta::get_entry_meta_by_field( $entry_id, $number_field_id, true );

  $myVal = sqrt($val );

  return $myVal;

}

In summary, you can have a shortcode almost anywhere you want, certainly without a page or view and the shortcode can do an infinite amount of things.

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 2045 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