Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active July 12, 2024 07:01
Show Gist options
  • Save wpmudev-sls/ce6c5898b55fa213b5597c82c48799f6 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/ce6c5898b55fa213b5597c82c48799f6 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Shortcode attribute in hidden field
<?php
/**
* Plugin Name: [Forminator Pro] Shortcode attribute in hidden field.
* Description: Renders shortcode attribute in hidden field value.
* Author: Prashant @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_attr', 10, 3 );
function wpmudev_change_hidden_field_data_attr( $entry, $module_id, $field_data_array ) {
$form_ids = array( 2960 ); // Please change the form ID.
if ( ! in_array( intval( $module_id ), $form_ids, true ) ) {
return;
}
foreach ( $field_data_array as $key => $value ) {
if ( 'hidden-1' === $value['name'] ) { // Please change the field ID.
Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value'] = sanitize_text_field( $_POST[ $value['name'] ] );
}
}
}
add_action( 'forminator_before_form_render', 'wpmudev_hidden_field_render_shortcode_attr', 10, 1 );
function wpmudev_hidden_field_render_shortcode_attr( $id ) {
if ( 2960 !== intval( $id ) ) { // Please change the form ID.
return;
}
add_filter( 'forminator_field_hidden_field_value', 'wpmudev_populate_shortcode_hidden_field', 10, 4 );
}
function wpmudev_populate_shortcode_hidden_field( $value, $saved_value, $field, $hidden_field ) {
if ( 'hidden-1' === $field['element_id'] ) { // Please change the field ID.
$data = array();
preg_match( '/\[forminator_form (.+?)\]/', get_the_content(), $data );
$data = array_pop( $data );
$data = explode( ' ', $data );
$params = array();
foreach ( $data as $d ) {
list( $opt, $val ) = explode( "=", $d );
$params[ $opt ] = trim( $val, '"' );
}
if ( ! empty( $params['x'] ) ) {
$value = sanitize_text_field( $params['x'] );
}
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment