Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Forminator Pro] - Bulk add options to a Select field
<?php
/**
* Plugin Name: [Forminator Pro] - Bulk add options to a Select field
* Plugin URI: https://premium.wpmudev.org/
* Description: Bulk add options to a Select field (as of 1.12.1.1)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1172200210290038
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// No need to do anything if the request is via WP-CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Bulk_Select_Options' ) ) {
class WPMUDEV_Forminator_Bulk_Select_Options {
// User defined settings
private $form_id = 26;
private $field_id = 'select-1';
// User defined options
private $options = array(
'value-1' => 'Label 1',
'value-2' => 'Label 2',
'value-3' => 'Label 3',
);
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Bulk_Select_Options();
}
return self::$_instance;
}
private function __construct() {
if ( ! defined( 'FORMINATOR_VERSION' ) || FORMINATOR_VERSION < '1.12' ) {
return;
}
$this->init();
}
public function init(){
add_filter( 'forminator_before_form_render', array( $this, 'wpmudev_forminator_before_form_render' ) );
}
public function wpmudev_forminator_before_form_render( $id ){
if( $this->form_id != $id ){
return;
}
add_filter( 'forminator_field_markup', array( $this, 'wpmudev_forminator_field_markup' ), 10, 2 );
}
public function wpmudev_forminator_field_markup( $html, $field ){
if( $field['element_id'] === $this->field_id ){
$markup = '';
foreach( $this->options as $key => $option ){
$markup .= '<option value="' . $key . '" data-calculation="0">' . $option .'</option>';
}
return str_replace( '</select>', $markup . '</select>', $html );
}
return $html;
}
}
add_action( 'plugins_loaded', function(){
return WPMUDEV_Forminator_Bulk_Select_Options::get_instance();
});
}
@amelsens
Copy link

amelsens commented Oct 1, 2021

This does indeed attach options to a select field, but afterwards the selected option is not saved together with the submitted form. Or can someone tell me how this can be done?
Thanks in advance.

@abhaypatel-new
Copy link

abhaypatel-new commented Apr 19, 2022

afterwards the selected option is not saved together with the submitted form. Or can someone tell me how this can be done?

@abhaypatel-new
Copy link

abhaypatel-new commented Apr 19, 2022

please help

@stupidchief
Copy link

stupidchief commented May 6, 2022

In my case I found that if you go to form settings and change value 'Multiple Option Value' to store 'Option Values' then this code stores the values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment