Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faisalahammad/9f6815380056c01b4cfceb7689611119 to your computer and use it in GitHub Desktop.
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.
<?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