Docs & Support

Learn about all the Formidable features and get support from our amazing customer success team.

Developer DocumentationFormidable HooksDisplay Data
frmpro_fields_replace_shortcodes

frmpro_fields_replace_shortcodes

This hook allows you to change the value displayed in emails, Views, or anywhere else that Formidable shortcodes are accepted.

Usage

add_filter('frmpro_fields_replace_shortcodes', 'my_custom_shortcode', 10, 4);
function my_custom_shortcode($replace_with, $tag, $atts, $field)

Parameters

  • $replace_with (string or array – the value to replace with before it’s sent through the other filters)
  • $tag (string – the shortcode name. “25″ in the example)
  • $atts (array – all the extra parameters added to the shortcode. Also includes entry_id item)
  • $field (object)

Examples

Check if current user is owner/creator

This code allows you to build if statements in a View to check if the current user is the creator of the entry. Use [if 100 show=ID is_current_user=1]content here[/if 100] to conditionally show/hide specific content based on whether or not the user currently viewing the display is the owner/creator of that entry. Change 100 in your shortcode to the ID of your user ID field. Then add this code to your theme functions.php or a new plugin without any changes:

add_filter('frmpro_fields_replace_shortcodes', 'frm_userid_shortcode1', 10, 4);
function frm_userid_shortcode1($replace_with, $tag, $atts, $field){
    if(isset($atts['is_current_user'])){
        global $user_ID;
        if ( !$user_ID || ( $user_ID && $user_ID != $replace_with ) ) {
            $replace_with = '';
        }
    }
    return $replace_with;
}

 

Show substring of an entry

Use this code to show a certain number of character from any field. In the example where 25 is a field ID, and the entry being returned is '123456', here is the usage for this shortcode.

  • [25 start="-4"] will only show the last four characters of a value. Returns '3456'
  • [25 length="4"] will show the first four characters of a value. Returns '1234'
  • [25 start="2" length="4"] will start on the third character, and show the next 4 characters. Returns '3456'
add_filter( 'frmpro_fields_replace_shortcodes', 'frm_substr_shortcode', 10, 4 );
function frm_substr_shortcode( $replace_with, $tag, $atts, $field ) {
    if ( isset( $atts['length'] ) ) {
        $start = isset( $atts['start'] ) ? $atts['start'] : 0;
        $replace_with = substr( $replace_with, $start, $atts['length'] );
    } elseif ( isset( $atts['start'] ) ) {
        $replace_with = substr( $replace_with, $atts['start'] );
    }
    return $replace_with;
}

 

Show an avatar from email

If you are using a User ID field, this function is not needed. Use this function if you are collecting email addresses from users who are not logged in, and would like to show an avatar using only the email address.
[25 avatar=40]
25 = the field ID
40 = the size of the avatar

add_filter('frmpro_fields_replace_shortcodes', 'frm_substr_shortcode', 10, 4);
function frm_substr_shortcode($replace_with, $tag, $atts, $field){
  if(isset($atts['avatar'])){
     $replace_with = get_avatar($replace_with, $atts['avatar']);
  }
  return $replace_with;
}

 

Insert multiple values into a shortcode

[25 shortcode=1] To display files uploaded with the multiple file option, you may need to integrate with another shortcode. For example, to play videos with the JW Player plugin, you need shortcodes that look like this: [jwplayer file="site.com/file"]

add_filter('frmpro_fields_replace_shortcodes', 'frm_make_shortcode', 10, 4);
function frm_make_shortcode($replace_with, $tag, $atts, $field){
  if(isset($atts['shortcode'])){
     $new_val = '';
     foreach((array)$replace_with as $v){
       if(is_numeric($v))
         $new_val .= '[jwplayer file="'. wp_get_attachment_url($v) .'"]';
     }
     return $new_val;
  }
  return $replace_with;
}

 

If you are using the multiple file upload option, and want the thumbnail to link to the full-sized image, add this to your theme functions.php or a new plugin. Then, in your View, use [25 link_full=1] but replace 25 with the ID of your field.

add_filter('frmpro_fields_replace_shortcodes', 'frm_make_shortcode', 10, 4);
function frm_make_shortcode($replace_with, $tag, $atts, $field){
  if(isset($atts['link_full'])){
     $new_val = '';
     foreach((array)$replace_with as $v){
       if(is_numeric($v)){
         $full = wp_get_attachment_image_src($v, 'full');
         $thumb = wp_get_attachment_image_src($v);
         $new_val .= '<a href="'. $full[0] .'"><img src="'. $thumb[0] .'" /></a>';
       }
     }
     return $new_val;
  }
  return $replace_with;
}

 

Only show first file from a multiple-image upload

Use [25 show_first=1] to show one file. Use [25 show_first=3] to show the first three files.

add_filter('frmpro_fields_replace_shortcodes', 'frm_show_first', 10, 4);
function frm_show_first($replace_with, $tag, $atts, $field){
  	if(isset($atts['show_first'])){
		extract( shortcode_atts(array(
			      'show_first' => '0',
		    ), $atts ) );
    	        $replace_with = array_filter((array)$replace_with);
	        $new_val = array_slice($replace_with, 0, $show_first);
    	        return $new_val;
        }
        return $replace_with;
}

 

Use [25 shortcode=1] To display individual links for each field option selected from a checkbox field.

add_filter('frmpro_fields_replace_shortcodes', 'frm_make_shortcode', 10, 4);
function frm_make_shortcode($replace_with, $tag, $atts, $field){
  if(isset($atts['shortcode'])){
     $new_val = '';
     foreach((array)$replace_with as $v){
       $new_val .= '<a href="testing.com/?paramname='.$v.'">'.$v.'</a>, '; //modify testing.com and paramname to whatever you would like. 
     }
     return $new_val;
  }
  return $replace_with;
}

 

Get value from a post ID

If you are using custom code to generate a list of posts for selection, you can use this to show the post title or any other info from the post that was selected in your field. After adding this function, you can get any information from that post that you would like.
[25 show_post=post_title]
[25 show_post=post_name] (slug)

add_filter('frmpro_fields_replace_shortcodes', 'frm_show_post_shortcode', 10, 4);
function frm_show_post_shortcode($replace_with, $tag, $atts, $field){
    if(isset($atts['show_post']) and is_numeric($replace_with)){
        $post = get_post($replace_with);
        if($post){
            if(isset($post->{$atts['show_post']}))
                $replace_with = $post->{$atts['show_post']};
            else
                $replace_with = get_post_meta($post->ID, $atts['show_post'], true);
        }
    }
    return $replace_with;
}

 

If you're using a data from entries checkbox field which pulls values from a form that creates posts, you can display links to these posts (in a View) with the code below. Just use [x link=true show=id] in a View, email, or confirmation message. Replace x with the field ID or key.

add_filter('frmpro_fields_replace_shortcodes', 'frm_dfe_link_to_post', 10, 4);
function frm_dfe_link_to_post($replace_with, $tag, $atts, $field){
	if ( isset($atts['link']) && $atts['link'] == 'true' && isset( $atts['show'] ) && $atts['show'] == 'id' ){
		if ( !is_array( $replace_with ) ){
			$replace_with = array( $replace_with );
		}
		
		$ids = array_filter( $replace_with,'is_numeric' ); // Let's make sure we're actually dealing with Post IDs
		if ( !empty( $ids ) and count( $ids ) == count( $replace_with ) ){
			$links = array();
			$post_ids = FrmProFieldsHelper::get_display_value( $replace_with, $field, array( 'show' => 'post_id', 'sep' => ',' ) ); 		
			foreach ( explode( ',', $post_ids ) as $i => $post_id ){
				// Using get_data_value just in case the display value is something different than the post title.  
				$links[] = sprintf( '<a href="%s" rel="nofollow">%s</a>', get_permalink( $post_id ), FrmProFieldsHelper::get_data_value( $replace_with[$i], $field, array() ) );
			}
			$replace_with = $links;
		}
	}
 	return $replace_with;
}

 

Add a case-sensitive like comparison

Use the code below to create a conditional statement that checks if a value contains a specific case-sensitive string. Use [if x case_sensitive_like="Your Value"]Content here[/if x] in your View, email, or success message. Replace x with the ID of your field.

add_filter('frmpro_fields_replace_shortcodes', 'frm_check_for_case_sensitive_like_value', 20, 4);
function frm_check_for_case_sensitive_like_value( $replace_with, $tag, $atts, $field ){
	if ( isset( $atts['case_sensitive_like'] ) && ! empty( $replace_with ) ) {
		if ( is_array( $replace_with ) ) {
			if ( ! in_array( $atts['case_sensitive_like'], $replace_with ) ) {
				$replace_with = '';
			}
		} else {
			if ( strpos( $replace_with, $atts['case_sensitive_like'] ) === false ) {
				$replace_with = '';
			}
		}
	}
	return $replace_with;
}

Use Base64 file info

If you are sending file uploads through an API, you may need more file information than just the url.
[25 show=id encoding=base64] will return the file contents
[25 show=id pathinfo=basename] will return the filename
[25 show=id fileinfo=mimetype] will return the MIME type

add_filter( 'frmpro_fields_replace_shortcodes', 'frm_get_file_info', 10, 4 );
function frm_get_file_info( $replace_with, $tag, $atts, $field ) {
  if ( ! is_numeric( $replace_with ) ) {
    return $replace_with;
  }

  if ( isset( $atts['encoding'] ) && $atts['encoding'] == 'base64' ) {
    $replace_with = base64_encode( file_get_contents( get_attached_file( $replace_with ) ) );
  } else if ( isset( $atts['pathinfo'] ) && $atts['pathinfo'] == 'basename' ) {
    $replace_with = pathinfo( get_attached_file( $replace_with ), PATHINFO_BASENAME );
  } else if ( isset( $atts['fileinfo'] ) && $atts['fileinfo'] == 'mimetype' ) {
    $finfo = finfo_open( FILEINFO_MIME_TYPE );
    $replace_with = finfo_file( $finfo, get_attached_file( $replace_with ) );
    finfo_close( $finfo );
  }
  return $replace_with;
}

Selectively strip HTML

The built-in striphtml=1 parameter will remove all HTML from that value. But if there are specific tags you would like to leave, you'll need to specify exactly what to allow. Your shortcode for this example will look like [25 leave_html='em,i,b']. You can find more detail on using wp_kses in the codex.

add_filter('frmpro_fields_replace_shortcodes', 'frm_filter_some_html', 10, 4);
function frm_filter_some_html($replace_with, $tag, $atts, $field){
    if ( isset($atts['leave_html']) ) {
        $tags = explode(',', $atts['leave_html']);
        $allowed_tags = array();
        foreach ( $tags as $tag ) {
          $allowed_tags[$tag] = array();
        }
        $replace_with = wp_kses($replace_with, $allowed_tags);
    }
    return $replace_with;
}

Greater than and less than

Use this to check if a field value is greater than X and less than Y. Use [if x greater_than="x" less_than="y"]Show this[/if x] in your View or email message.

add_filter('frmpro_fields_replace_shortcodes', 'frm_greater_than_less_than', 10, 4);
function frm_greater_than_less_than($replace_with, $tag, $atts, $field){
    if ( isset ( $atts['greater_than'] ) && isset( $atts['less_than'] ) ) {
		if ( $replace_with > $atts['greater_than'] && $replace_with < $atts['less_than'] ) {
			// do nothing if there's a match
		} else {
			$replace_with = '';
		}
    }
    return $replace_with;
}

Check if value is not equal to several values

Use this to check if a field value is not equal to several values. Use [if x not_equal_multiple="value1,value2,value3"]content here[/if x] in your View or email message.

add_filter('frmpro_fields_replace_shortcodes', 'frm_not_equal_to_multiple_values', 10, 4);
function frm_not_equal_to_multiple_values( $replace_with, $tag, $atts, $field ) {
    if ( isset ( $atts['not_equal_multiple'] ) ) {
        $check_values = explode( ',', $atts['not_equal_multiple'] );
        foreach ( $check_values as $check_value ) {
	    if ( $replace_with == $check_value ) {
		$replace_with = '';
                break;
	    }
        }
    }
    return $replace_with;
}
Was this article helpful? *

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

In this article

    We have a small, but amazing team of dedicated people who are committed to helping you achieve your goals and project requirements.


    Copyright © 2025 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.

    Complete your purchase
    Special offer unlocked.
    Get 55% OFF!
    Complete Purchase
    Join 400,000+ using Formidable Forms to create form-focused solutions fast. Get Formidable Forms