Created
February 21, 2023 12:51
-
-
Save adczk/d488c7a69b8ee6223303267583fd01c3 to your computer and use it in GitHub Desktop.
[Forminator] populate select field with posts and calculation value
This file contains hidden or 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] populate select field with posts and calculation value | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: Populates select field with posts of selected type (label = post title, | |
| * value = post ID ) and sets calculation values for select options based on selected ACF custom post field | |
| * Author: adczk (with adjustments of Prashant Singh) | |
| * Author URI: https://premium.wpmudev.org/ | |
| * License: GPLv2 or later | |
| * | |
| * Tested with Forminator 1.23.0 | |
| * | |
| * Based directly on this with just a few modifications | |
| * https://gist.github.com/wpmudev-sls/8f5a660e3a8895f4724b7485ad4befea | |
| * | |
| * config explained in code comment | |
| * | |
| * note: select field must be added manually to the form first | |
| * it doesn't matter if it has options set and what are they | |
| * it needs to have calculations enabled | |
| * | |
| */ | |
| add_filter( 'forminator_cform_render_fields', 'forminator_select_posts_list', 10, 2 ); | |
| function forminator_select_posts_list( $wrappers, $form_id ) { | |
| $allowed_forms = array( 16134 ); // IDs of forms to apply to (separated by comma if more) | |
| // which select fields to fill in with what post types | |
| // array key (left) = select field ID | |
| // array value (right) = post type name | |
| $select_fields_data = array( | |
| 'select-1' => 'post', | |
| ); | |
| $acf_field = 'price'; // name of ACF field to fetch option calculation value from. | |
| ##### DO NOT EDIT BELOW #####; | |
| if ( ! in_array( $form_id, $allowed_forms) ) { | |
| return $wrappers; | |
| } | |
| foreach ( $wrappers as $wrapper_key => $wrapper ) { | |
| if ( ! isset( $wrapper[ 'fields' ] ) ) { | |
| continue; | |
| } | |
| if ( | |
| isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) && | |
| ! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) | |
| ) { | |
| error_log( print_r( $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] , true ) ); | |
| $pargs = array( | |
| 'numberposts' => -1, | |
| 'orderby' => 'date', | |
| 'order' => 'DESC', | |
| 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] | |
| ); | |
| $posts = get_posts( $pargs ); | |
| if ( ! empty( $posts ) ) { | |
| $new_options = array(); | |
| foreach( $posts as $post ) { | |
| $new_options[] = array( | |
| 'label' => $post->post_title, | |
| 'value' => $post->ID, | |
| 'limit' => '', | |
| 'key' => forminator_unique_key(), | |
| 'calculation' => get_field( $acf_field, $post->ID ) | |
| ); | |
| $opt_data['options'] = $new_options; | |
| } | |
| //$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options; | |
| $select_field = Forminator_API::get_form_field( $form_id, $wrapper['fields'][0]['element_id'], true ); | |
| if( $select_field && !empty( $opt_data ) ){ | |
| Forminator_API::update_form_field( $form_id, $wrapper['fields'][0]['element_id'], $opt_data ); | |
| $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options; | |
| } | |
| } | |
| } | |
| } | |
| return $wrappers; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment