Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @getpressed,

    Trust you are doing good and thank you for reaching out to us.

    Can you please share the custom code that you are using so that we can take a closer look and help you with this?

    Looking forward to hearing back from you.

    Kind Regards,
    Nebu John

    Thread Starter getPressed

    (@getpressed)

    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();
        });
    
    }
    
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @getpressed

    Can you test this version?

    https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b

    You need to update the custom post type and field line 6, also if you are looking to apply to a specific form you can manage some validations using $model_id & $data[‘form_id’]

    Best Regards
    Patrick Freitas

    Thread Starter getPressed

    (@getpressed)

    Thanks, Patrick. This solves the problem with the options added programmatically not saving; really helpful!

    I’d need to get a better handle on how to use $model_id to target this behaviour at specific forms, but I think the ideal approach would be to use a class to flag fields that this should apply to, so that it can be set through the Edit Form ui in the dashboard.

    I know it’s possible to add a class to a field’s container using Edit Field > Styling, the “Additional CSS Classes” setting. Is it possible to target this behaviour that way?

    Thanks again for your help above. I’ve seen a couple of other users ask this question elsewhere without getting a solution, so really appreciate the code you provided.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @getpressed

    The code needs to know form ID but even though we could possibly determine form ID “dynamically” based on whether there is certain class or not, this would have to be done after form fields are “rendered” (not necessarily as in “displayed on page” but rather as in “form is built”).

    But that would be after this code is already run so it’s a kind of a “vicious circle”, I’m afraid.

    A way simpler solution could be to actually set that on a different level:
    – each form has to be displayed “somewhere” – on some page
    – if you add some custom field to such post/page you could read it’s value
    – and based on that set form ID in the code and actually execute entire code.

    The simplest application of that could be to do it this way:

    – add custom field (e.g. “form_id”) and put a form ID into it – that would let you do this directly in post/page editor
    – then add a code (e.g. via mu plugin) into the footer of the page that would read that custom field value
    – and if it’s set it would add that custom code and use that read value as form_id

    It’s not exactly what you suggested but would at least let you control if the code should be applied via a “sort of UI”.

    Best regards,
    Adam

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @getpressed ,

    We haven’t heard from you for a while now, so it looks like you don’t need our assistance anymore.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘CPT Titles as Select Options in Forminator form’ is closed to new replies.