• Resolved hueskedotdigital

    (@hueskedotdigital)


    Hey,

    I’m trying to fill a select field with custom post data (e.g. title) for my form. These should also be sent later in the confirmation e-mail.

    I can’t do this in the normal way, do you have any help (custom script)?

    Tobias

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @hueskedotdigital,

    I hope you’re doing great today!

    Here’s an example of the script for populating {select-1} field with the post titles:

    <?php
    
    add_filter(
        'forminator_cform_render_fields',
        function( $wrappers, $form_id ) {
    
            $allowed_forms = array (
                2268,
            );
    
            if ( ! in_array( $form_id, $allowed_forms) ) {
                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' ] ] , 'numberposts' => -1 ) );
    
                    if ( ! empty( $posts ) ) {
                        $new_options = array();
                        $opt_data    = array();
                        foreach( $posts as $post ) {
                            $new_options[] = array(
                                'label' => $post->post_title,
                                'value' => $post->ID,
                                'limit' => '',
                                'key'   => forminator_unique_key(),
                            );
                            $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 ) {
                            Forminator_API::update_form_field( $form_id, $wrapper[ 'fields' ][ 0 ][ 'element_id' ], $opt_data );
                        }
                    }
                }
            }
    
            return $wrappers;
        },
        10,
        2
    );

    – you’ll need to replace “2268” with your form ID, and replace “select-1” with the actual field ID.

    Save the code as a PHP file, for example “forminator-populate-select-field-with-posts.php“, and upload it to /wp-content/mu-plugins/ directory on the server, in order for it to run as a must use plugin.

    You then can send the field value the same way, as any other fields, by using the macros in the Email Notifications section:

    https://prnt.sc/-FWjg9ucX1Bb

    I hope this helps. Please let us know if you have any questions.

    Best Regards,
    Dmytro

    Thread Starter hueskedotdigital

    (@hueskedotdigital)

    Thank you! Works. Any chance to get this into UI in future?

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @hueskedotdigital

    I have doubts we will implement this in future updates as this is already available functionality in Forminator when it comes to HTML. This field allows you to select “Embed Post/Page Title”.

    Later on in your email, you can call {html-1} field which will call {embed_title}, or you can simply place in email notification {embed_title}.

    Is there any reason to call the post title into {select-1} if that will call only 1 title from the post URL where the end user will land?

    Kind Regards,
    Kris

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @hueskedotdigital,

    We haven’t heard from you in a while, we will go ahead and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Kind regards,
    Zafer

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.