Last active
September 5, 2023 15:38
-
-
Save patrickfreitasdev/d7ad926561db4b598baeaf66478d790b to your computer and use it in GitHub Desktop.
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 | |
add_filter( | |
'forminator_cform_render_fields', | |
function( $wrappers, $model_id ) { | |
if( $model_id != 1441 ){ | |
return $wrappers; | |
} | |
$select_fields_data = array( | |
'select-3' => 'post', | |
); | |
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' ] ] ) | |
) { | |
$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) ); | |
if ( ! empty( $posts ) ) { | |
$new_options = array(); | |
$opt_data = array(); | |
foreach( $posts as $post ) { | |
$new_options[] = array( | |
'label' => $post->post_title, | |
'value' => $post->post_title, | |
'limit' => '', | |
'key' => forminator_unique_key(), | |
); | |
$opt_data['options'] = $new_options; | |
} | |
$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true ); | |
if( $select_field ){ | |
if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){ | |
Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data ); | |
$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options; | |
} | |
} | |
} | |
} | |
} | |
return $wrappers; | |
}, | |
10, | |
2 | |
); | |
add_filter( | |
'forminator_replace_form_data', | |
function( $content, $data, $fields ) { | |
if( $data['form_id'] != 361 ){ | |
return $content; | |
} | |
if ( ! empty( $content ) ) { | |
return $content; | |
} | |
$form_fields = Forminator_API::get_form_fields( $data['form_id'] ); | |
$data_field = ''; | |
foreach($data as $key => $value){ | |
if ( strpos( $key, 'select' ) !== false ) { | |
$values = ''; | |
$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null; | |
if ( ! is_null( $field_value ) ) { | |
$fields_slugs = wp_list_pluck( $form_fields, 'slug' ); | |
$field_key = array_search( $key, $fields_slugs, true ); | |
$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] ) | |
? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' ) | |
: array(); | |
if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) { | |
return sanitize_text_field( $_POST[ $key ] ); | |
} | |
} | |
} | |
} | |
return $content; | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @patrickfreitasdev, I'm looking to achive
-> dynamically fill a select field values
-> set one of the values as default, depending on the page/post viewed
I'm using the code above and it works almost as expected. The only problem I have is, that I want to set one of the options as default, depending on which post the form is displayed. I tried
but this doesnt work. Any idea on how to achive this?
Thanks, Adam