Skip to content

Instantly share code, notes, and snippets.

@adczk
Last active February 11, 2022 15:15
Show Gist options
  • Select an option

  • Save adczk/9b1b95783caeb75a1848fc9028caf5d1 to your computer and use it in GitHub Desktop.

Select an option

Save adczk/9b1b95783caeb75a1848fc9028caf5d1 to your computer and use it in GitHub Desktop.
Forminator - prefill text field with post title (works with Woo Commerce products too)
<?php
/**************************************
*
* Forminator - prefill text field with post title
*
*
* by Adam/WPMU
*
*
* use as MU plugin
* tested with Forminator 1.15.11 only
*
* configure $form_id and $title_field in code below, according to comments
*
***************************************/
add_action( 'forminator_before_form_render', 'wpmu_forminator_woo_title_add_filter', 10, 1);
function wpmu_forminator_woo_title_add_filter( $id ) {
$form_id = '13376'; // ID of the form that should be prefilled;
if ( $form_id == $id ) {
add_filter( 'forminator_field_text_markup', 'wpmu_forminator_woo_title', 10, 2);
}
}
function wpmu_forminator_woo_title( $html, $field ) {
$title_field = "text-1"; // ID of a text field to be prefilled with product title
if ( $title_field === $field['element_id'] ) {
$product_title = forminator_get_post_data( 'post_title' ); //temporary
$html = str_replace( 'value=""', 'value="' . $product_title . '"', $html );
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment