This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
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 last four characters
Use this code to only show the last four characters of any field. For example, [25 substr="-4"] will only show the last four characters of a value (25 is a field ID).
add_filter('frmpro_fields_replace_shortcodes', 'frm_substr_shortcode', 10, 4);
function frm_substr_shortcode($replace_with, $tag, $atts, $field){
if(isset($atts['substr'])){
$replace_with = substr($replace_with, $atts['substr']);
}
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;
}
Link thumbnail to full size images with multiple uploads
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;
}
Insert a link for each field option into a shortcode
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;
}
Link to posts with Data from Entries field
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
When using this code snippet, it will check if the file is protected when using the base64 setting or the fileinfo=mimetype setting which also requires the file protection be lifted temporarily. If it is, it will remove the protection it for a brief moment, get the base64 value, then set the protection back.
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;
}
$use_base64 = isset( $atts['encoding'] ) && $atts['encoding'] === 'base64';
$use_path_info = ! $use_base64 && isset( $atts['pathinfo'] ) && $atts['pathinfo'] == 'basename';
$use_mimetype = ! $use_base64 && ! $use_path_info && isset( $atts['fileinfo'] ) && $atts['fileinfo'] == 'mimetype';
if ( ! $use_base64 && ! $use_path_info && ! $use_mimetype ) {
return $replace_with;
}
$file_path = get_attached_file( $replace_with );
$requires_unprotected_file = $use_base64 || $use_mimetype;
$change_file_protection_temporarily = $requires_unprotected_file && FrmProFileField::WRITE_ONLY === FrmProFileField::get_chmod( array( 'file' => $file_path ) );
if ( $change_file_protection_temporarily ) {
FrmProFileField::chmod( $file_path, FrmProFileField::READ_ONLY );
}
if ( $use_base64 ) {
$replace_with = base64_encode( file_get_contents( $file_path ) );
} elseif ( $use_path_info ) {
$replace_with = pathinfo( $file_path, PATHINFO_BASENAME );
} elseif ( $use_mimetype ) {
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$replace_with = finfo_file( $finfo, $file_path );
finfo_close( $finfo );
}
if ( $change_file_protection_temporarily ) {
FrmProFileField::chmod( $file_path, FrmProFileField::WRITE_ONLY );
}
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;
}
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;
}
Change the output for a field type
Change the displayed value based on the field type.
add_filter( 'frmpro_fields_replace_shortcodes', 'display_field_value_with_shortcode', 10, 4 );
function display_field_value_with_shortcode( $value, $tag, $atts, $field ) {
if ( $field->type != 'signature' ) {
return $value;
}
$value = '<div class="my_custom_class">' . $value . '</div>';
return $value;
}
Show comma-separate values as a list
Use this code to display each item in a comma-separated list in a text field separately. Add the show_list=1 param to show the items as a list, with each item on its own line. Use the sep param to customize the separator.
Usage:
- [100 show_list=1] -- shows each item on a separate row
- <ul><li>[100 show_list=1 sep="</li><li>"]</li></ul>-- show each item in an unordered list
add_filter( 'frmpro_fields_replace_shortcodes', 'frm_show_list', 10, 4 );
function frm_show_list( $replace_with, $tag, $atts, $field ) {
if ( ! isset( $atts['show_list'] ) ) {
return $replace_with;
}
$sep = isset( $atts['sep'] ) ? $atts['sep'] : '<br>';
return str_replace( ', ', $sep, $replace_with );
}
Display multi-select option values
Dropdown and checkbox fields allow selecting multiple options. This example will display multi-selected option values in a view.
add_filter( 'frmpro_fields_replace_shortcodes', 'frm_display_option_values_with_pattern', 10, 4 );
function frm_display_option_values_with_pattern( $replace_with, $tag, $atts, $field ) {
if ( isset ( $atts['pattern'] ) ) {
if( $replace_with ) {
$pattern = $atts['pattern'];
if( is_array( $replace_with ) ){
foreach($replace_with as $key => $value) {
$replace_with[$key] = str_replace( '{}', $value, $pattern );
}
if ( isset( $atts['sep'] ) ) {
$replace_with = implode( $atts['sep'], $replace_with );
} else {
$replace_with = implode( '', $replace_with );
}
} else {
$replace_with = str_replace( '{}', $replace_with, $pattern );
}
}
}
return $replace_with;
}
The example above was taken from this article where you can find more details about the example.
Reverse entry order in a repeater
Note: This is now a built-in feature of Formidable Pro.
Use [foreach 5651 foreach_entry_order=desc] to reverse the entry order when displaying data from a repeater field. Replace 5651 with the field ID of your Repeater field.
add_filter('frmpro_fields_replace_shortcodes', 'reverse_entry_order_repeater', 10, 4);
function reverse_entry_order_repeater( $value, $tag, $atts, $field ) {
if ( ! empty( $atts['foreach_entry_order'] ) && 'desc' === $atts['foreach_entry_order'] ) {
arsort( $value );
}
return $value;
}