Created
June 21, 2023 20:59
-
-
Save alfredo-wpmudev/4122134d81dbd47075547b9b971a98e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Bulk add options to a Select field Church Groups | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Load data from specific table on the Database and populating the select field. | |
* Author: Alessandro Kaounas @ WPMUDEV, Paweł Pela @ WPMUDEV, Alfredo Loyola @WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* 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 | |
//Set your Form ID here | |
private $form_id = 24249; | |
//Indicate the select field that will receive the data | |
private $field_id = 'select-1'; | |
// User defined options | |
private $options = array(); | |
private $options_data = array(); | |
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() { | |
/*Customize your data to load here */ | |
global $wpdb; | |
$sql = $wpdb->prepare("SELECT `GroupName` FROM `churchgroup` ORDER BY GroupName ASC"); | |
$partners = $wpdb->get_results($sql); | |
foreach($partners as $partner) { | |
$this->options[$partner->GroupName] = $partner->GroupName; | |
} | |
/*Stop customizing */ | |
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 = ''; | |
$new_options = array(); | |
foreach( $this->options as $key => $option ){ | |
$markup .= '<option value="' . $key . '" data-calculation="0">' . $option .'</option>'; | |
$new_options[] = array( | |
'label' => $option, | |
'value' => $key, | |
'limit' => '', | |
'key' => forminator_unique_key(), | |
); | |
} | |
$this->options_data['options'] = $new_options; | |
$select_field = Forminator_API::get_form_field( $this->form_id, $field['element_id'], true ); | |
if( $select_field ){ | |
Forminator_API::update_form_field( $this->form_id, $field['element_id'], $this->options_data ); | |
} | |
return str_replace( '</select>', $markup . '</select>', $html ); | |
} | |
return $html; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
return WPMUDEV_Forminator_Bulk_Select_Options::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment