Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active February 17, 2024 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickfreitasdev/6196a5fd4cdebdfa5831868edcbed6a1 to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/6196a5fd4cdebdfa5831868edcbed6a1 to your computer and use it in GitHub Desktop.
<?php
add_filter(
'forminator_cform_render_fields',
function( $wrappers, $model_id ) {
if( $model_id != 847 ){
return $wrappers;
}
$select_fields_data = array(
'select-1' => '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' ] ],
'category_name' => 'career'
)
);
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'] != 847 ){
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
);
@whynotadv
Copy link

whynotadv commented Aug 23, 2022

How do I use this code? Where do I find the model_id? I'm looking for a way to do this for my home builder client so I don't have to constantly manage the select fields values when model homes are added to the website. I created a custom post type of model homes 'models' and I want to populate the Forminator form select field {select-4} on a the form. This form is added to the contact us page and shows a select drop down box with model home page titles in it. I'm sure it's simple, but I cannot for the life of me figure out what some of these variables do. Can you help me?

@patrickfreitasdev
Copy link
Author

Hi @whynotadv

We would need to take a closer look, this code couldn't be the correct or best approach for you,

Could you create a ticket at https://wordpress.org/support/plugin/forminator/#new-topic-0 if you are using the Free version so we can take a closer look?

@whynotadv
Copy link

whynotadv commented Aug 27, 2022 via email

@whynotadv
Copy link

whynotadv commented Aug 29, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment