Thanks for the response. Here’s the code I’m using:
<?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 = 1080;
private $field_id = 'select-1';
// User defined options
private $options = array(
'none' => 'None',
);
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;
}
$post_type_query = new WP_Query(
array (
'post_type' => 'club',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
),
)
);
$posts_array = $post_type_query->posts;
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );
$i = 0;
foreach( $post_title_array as $key => $value ){
$post_title[] = $value;
array_push($this->options, $post_title[$i]);
$i++;
}
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();
});
}