• Resolved stoodlee

    (@stoodlee)


    I want to dynamically pre populate a form input field with the page/post title using a hidden field. I found an old post in this forum with a function to put a hidden field value in the text field. The hidden field holds the page title. But I am lost on how to make it work.
    The code is
    (function() {
    const hidden = document.querySelector(‘#forminator-module-4000 #hidden-1’);
    document.querySelector(“#forminator-module-4000 #textarea-2”).value = hidden.value;
    })();

    I am using the WPCode plugin to put the code in the page. I can see it in Page Source. The form and field IDs are what I am using.
    Normally only people who login to the site can see the form (to make an enquiry). I have removed that restriction which means the Name and Email fields on the form are not prefilled. The Page is
    https://www.aaq.org.au/13-truss-tube-dobsonian/

    I have exported the Form and placed at https://pastebin.com/n7d9xBCV

    Can someone help?

    • This topic was modified 1 year, 1 month ago by stoodlee.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @stoodlee

    I hope you are doing well.

    You don’t need custom coding unless it is something more complex, but for hidden field, you can use the native option “Embed Post/Page Title – The title of the site page where the form is embedded.

    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#default-value

    We do have a small bug, but to bypass the bug enable the load using Ajax on Form > Behaviour > Rendering > Ajax loading.

    Let us know if it fits your needs.
    Best Regards
    Patrick Freitas

    Thread Starter stoodlee

    (@stoodlee)

    Hi. Thanks for the quick response. I had used the native option “Embed Post/Page Title” for the Hidden Field.
    But how do I then get that value into another input text field?
    I want the field Textarea-2 pre-populated with the Post Title.
    Here are screenshots of the form and fields:
    https://snipboard.io/aA2pJ8.jpg

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi

    By default, you can only pre-populate the post/page title into the HTML field by {embed_title} value. I asked our SLS Team if there is a way to fill textarea with page title. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again

    Please:

    add_action( 'forminator_before_form_render', 'wpmudev_post_data_form_check', 10, 5 );
    function wpmudev_post_data_form_check( $id, $form_type, $post_id, $form_fields, $form_settings ) {
    	if ( 939 == $id ) { //Please change the form ID
    		add_filter( 'forminator_field_text_markup', 'wpmudev_add_post_page_title_text', 10, 2 );
    	}
    }
    
    function wpmudev_add_post_page_title_text( $html, $field ) {
        if ( $field[ 'element_id' ] != 'textarea-2' ) {
            return $html;
        }
        $post_id    = get_the_ID();
        $title      = forminator_get_post_data( 'post_title', $post_id );
        $html       = str_replace('</textarea>', "$title</textarea>", $html);
        return $html;
    }

    You will need to change 939 to your form ID and textarea-2 to your textarea field.

    Kind Regards,
    Kris

    Thread Starter stoodlee

    (@stoodlee)

    Thank you very much for your fantastic help! The MU Plugin code works perfectly. For people watching, or who come later for this answer, here is my documentation of the solution:

    SOLUTION – to pre-populate a Form text field with the Page/Post Title
    Requires “Must Use” WordPress plugin as described below
    STEPS

    • Make sure to run a full site backup before you do anything
    • PHP Code (note you MUST update with your Form ID and Text Field Name):
    <?php
    /**
     * Plugin Name: WPMUDEV Forminator Form addition 
     * Description: Pre-populate Form field Textarea-? with the Post/Page Title
     *              The Post/Page title is put in a hidden field using standard Forminator settings
     *              This code loads the hidden field value into the field Textarea-2
     * Author:      WPMU DEV
     * Reference:   https://wordpress.org/support/topic/fill-a-field-with-the-current-page-post-title-2/  (october 2023)
     * License:     GNU General Public License v3 or later
     * License URI: http://www.gnu.org/licenses/gpl-3.0.html
     */
    
    add_action( 'forminator_before_form_render', 'wpmudev_post_data_form_check', 10, 5 );
    
    function wpmudev_post_data_form_check( $id, $form_type, $post_id, $form_fields, $form_settings ) {
    	if ( 4000 == $id ) { //Set number to your form ID
    		add_filter( 'forminator_field_text_markup', 'wpmudev_add_post_page_title_text', 10, 2 );
    	}
    }
    
    function wpmudev_add_post_page_title_text( $html, $field ) {
        if ( $field[ 'element_id' ] != 'textarea-2' ) {  //Put text field name in your form ID
            return $html;
        }
        $post_id    = get_the_ID();
        $title      = forminator_get_post_data( 'post_title', $post_id );
        $html       = str_replace('</textarea>', "$title</textarea>", $html);
        return $html;
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Fill a field with the current Page/post Title’ is closed to new replies.