Created
April 13, 2025 05:42
-
-
Save faisalahammad/9f6815380056c01b4cfceb7689611119 to your computer and use it in GitHub Desktop.
This snippet auto-appends an asterisk to the placeholder text of required fields in Gravity Forms. It ensures that only fields with placeholders are modified, and prevents double-adding asterisks.
This file contains 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 | |
function gw_required_placeholder_asterisk( $form ) { | |
foreach ( $form['fields'] as &$field ) { | |
// Skip fields that don't support placeholders | |
if ( ! is_callable( [ $field, 'get_entry_inputs' ] ) ) { | |
continue; | |
} | |
// Single input fields | |
if ( is_array( $field->inputs ) ) { | |
$inputs = $field->inputs; | |
foreach ( $inputs as &$input ) { | |
if ( $field->isRequired && isset( $input['placeholder'] ) ) { | |
$input['placeholder'] = gw_append_asterisk( $input['placeholder'] ); | |
} | |
} | |
$field->inputs = $inputs; | |
} else { | |
if ( $field->isRequired && isset( $field->placeholder ) ) { | |
$field->placeholder = gw_append_asterisk( $field->placeholder ); | |
} | |
} | |
} | |
return $form; | |
} | |
function gw_append_asterisk( $text ) { | |
// Avoid double-adding an asterisk | |
if ( substr( $text, -1 ) !== '*' ) { | |
$text .= ' *'; | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment