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
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
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
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
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;
}