Last active
July 1, 2024 05:53
-
-
Save wpmudev-sls/d0dcbeba3378a4e7db9cf022cca35697 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Copy select field options to other select fields.
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] Copy select field options to other select fields. | |
* Description: Allows to copy options of a select field to other select fields across different forms. | |
* Author: Prashant @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
add_filter( 'forminator_cform_render_fields', function( $wrappers, $form_id ) { | |
// Please add the form IDs and respective select field IDs where you want to copy the options. | |
$allowed_forms = array( 45717 => 'select-1', 45718 => 'select-1' ); | |
$parent_form = 2960; // Please change this to the parent form ID. | |
$parent_field = 'select-1'; // Please change this to the parent field ID. | |
if ( ! in_array( intval( $form_id ), array_keys( $allowed_forms ), true ) ) { | |
return $wrappers; | |
} | |
foreach ( $wrappers as $wrapper_key => $wrapper ) { | |
if ( ! isset( $wrapper['fields'] ) ) { | |
continue; | |
} | |
foreach ( $wrapper['fields'] as $field_key => $fields ) { | |
if ( $allowed_forms[ $form_id ] === $fields['element_id'] ) { | |
$opt_data = array(); | |
$parent_field = Forminator_API::get_form_field( $parent_form, $parent_field, true ); | |
$child_field = Forminator_API::get_form_field( $form_id, $fields['element_id'], true ); | |
if ( $parent_field ) { | |
$wrappers[ $wrapper_key ]['fields'][ $field_key ]['options'] = $parent_field['options']; | |
$opt_data['options'] = $parent_field['options']; | |
} | |
if ( $child_field ) { | |
Forminator_API::update_form_field( $form_id, $fields['element_id'], $opt_data ); | |
} | |
} | |
} | |
} | |
return $wrappers; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment