Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created October 15, 2019 14:49
Show Gist options
  • Save wpmudev-sls/dc9ab640bf675b274ff12acfba3965db to your computer and use it in GitHub Desktop.
Save wpmudev-sls/dc9ab640bf675b274ff12acfba3965db to your computer and use it in GitHub Desktop.
[Forminator] - Auto Populate Hidden Fields. Adds a new option in Hidden's Edit Field popup under the Advanced tab. From there you can choose to use the Custom Value of the hidden field as the query var of the url, of which value we want
<?php
/**
* Plugin Name: [Forminator] - Auto Populate Hidden Fields
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds a new option in Hidden's Edit Field popup under the Advanced tab. From there you can choose to use the Custom Value of the hidden field as the query var of the url, of which value we want
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
/**
* This mu-plugins allows you to auto populate your form's hidden fields with a value from the page's url.
* 1. Make sure you have set the hidden's field `Default Value (optional)` to `Custom Option`
* 2. In the `Custom Value` field enter the query var from the url that you need to take the value from. eg yoursite.com/pagewithform/?the-var=the value
* Use the `the-var` in that field in order to populate the field with `the value`
* 3. In the `ADVANCED` tab, make sure you have enabled the custom option `Populate Field from URL`
*/
defined( 'WPINC' ) || die;
if ( ! class_exists( 'WPMUDEV_Forminator_AutoPopulate_Hiddens' ) ) {
class WPMUDEV_Forminator_AutoPopulate_Hiddens {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_AutoPopulate_Hiddens();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'forminator_field_hidden_general_settings', array( $this, 'field_settings' ) );
add_filter( 'forminator_field_hidden_field_value', array( $this, 'maybe_populate_field' ), 10, 4 );
}
public function maybe_populate_field( $value, $saved_value, $field, $hidden_field ) {
if (
'custom_value' == $saved_value &&
Forminator_Field::get_property( 'auto_populate_field', $field ) &&
isset( $_REQUEST[ $value ] )
)
{
$value = sanitize_text_field( $_REQUEST[ $value ] );
}
return $value;
}
public function field_settings( $settings ) {
$populate_hidden_field = array(
'id' => 'populate-field',
'type' => 'Toggle',
'name' => 'auto_populate_field',
'hide_label' => false,
'label' => __( 'Populate Field from URL. Enable this option in order to use the Custom Value as the desired query var' ),
'className' => 'populate-field'
);
array_push( $settings, $populate_hidden_field );
return $settings;
}
}
if ( ! function_exists( 'wpmudev_forminator_autopopulate_hiddens' ) ) {
function wpmudev_forminator_autopopulate_hiddens() {
return WPMUDEV_Forminator_AutoPopulate_Hiddens::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_autopopulate_hiddens', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment