Last active
February 17, 2024 05:20
-
-
Save patrickfreitasdev/6196a5fd4cdebdfa5831868edcbed6a1 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 != 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 | |
); |
Hi Patrick,
I already have an open support ticket here:
https://wordpress.org/support/topic/automatically-pull-in-cpt-post-titles-into-a-select-field/
Thank you,
Kenny
On Sat, Aug 27, 2022 at 12:32 PM Patrick de Freitas < ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi @whynotadv <https://github.com/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?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/6196a5fd4cdebdfa5831868edcbed6a1#gistcomment-4281728>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA33JG6BXQEOZBRI7X4PCFDV3I7IBANCNFSM57LVUMHQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Kenny Berwager
—————————————
Owner | Designer | Marketer
—————————————
Why Not Advertising, LLC
P.O. Box 290, East Berlin, PA 17316
717-630-1240
www.whynot.company
Hi Patrick,
I updated the code and it still won't load? The CPT is correct and the ID
for the form is correct as well. Anything that is missing?
Thanks,
Kenny
*Kenny Berwager*
*Owner | Designer | Marketer*
*Why Not Advertising, LLC*
P.O. Box 290, East Berlin, PA 17316
*Phone:* 717.630.1240
www.whynot.company
…On Sat, Aug 27, 2022 at 12:32 PM Patrick de Freitas < ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hi @whynotadv <https://github.com/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?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/6196a5fd4cdebdfa5831868edcbed6a1#gistcomment-4281728>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA33JG6BXQEOZBRI7X4PCFDV3I7IBANCNFSM57LVUMHQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?