Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/d6f5bfbf17af7d5edf972e863aa0e01a to your computer and use it in GitHub Desktop.
Save wpmudev-sls/d6f5bfbf17af7d5edf972e863aa0e01a to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Forminator] - Filter value for selectable fields
* Plugin URI: https://premium.wpmudev.org/
* Description: This snippet filters the value of the selectable fields, radio, checkboox and select fileds.
* Task: SLS-1994
* Author: Prashant Singh @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_filter( 'forminator_replace_form_data', 'wpmudev_get_fields_value' ,10 ,3);
function wpmudev_get_fields_value($content, $data, $fields){
$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
$data_field = '';
foreach($data as $key => $value){
if ( strpos( $key, 'radio' ) !== false
|| strpos( $key, 'select' ) !== false
|| strpos( $key, 'checkbox' ) !== false
) {
$values = '';
$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
if ( ! is_null( $field_value ) ) {
$fields_slugs = wp_list_pluck( $form_fields, 'slug' );
$field_key = array_search( $key, $fields_slugs, true );
$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
: array();
$selected_values = is_array( $field_value ) ? $field_value : array( $field_value );
$values = implode( ', ', array_keys( array_intersect( array_flip( $field_options ), $selected_values ) ) );
}
if ( is_array( $values ) ) {
$values = implode( ', ', $values );
}
if( is_array( $data[$key] ) ) {
$data_field = implode( ', ', $data[$key] );
} else {
$data_field = $data[$key];
}
$content = str_replace( $values, $data_field, $content );
}
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment