Skip to content

Instantly share code, notes, and snippets.

@rniswonger
Last active Mar 27, 2021
Embed
What would you like to do?
Gravity Forms: Replace the submit <input> with a <button>
/**
* Gravity Forms: Filters the submit button.
* Replaces the <input> button with a <button> while maintaining attributes from original <input>.
*
* @param string $button Contains the <input> tag to be filtered.
* @param object $form Contains all the properties of the current form.
*
* @return string The filtered button.
*
* GF provides sample code for doing this under Examples at the following link but this dodn't work in my environment.
* https://docs.gravityforms.com/gform_submit_button/
*/
add_filter(
'gform_submit_button',
function ( $button, $form ) {
$button_xml = simplexml_load_string( $button );
$button_attributes = '';
$button_text = 'Submit';
// combine the existing button attributes into a new string
foreach ( $button_xml->Attributes() as $key => $value ) :
// exclude the "type" and "value" attributes
if ( $key !== 'type' && $key !== 'value' ) :
$button_attributes .= sprintf( ' %s=\'%s\'', $key, $value );
endif;
// store the "value" attributes value
if ( $key === 'value' ) :
$button_text = $value;
endif;
endforeach;
$new_button = sprintf( '<button %s>%s</button>', $button_attributes, $button_text );
return $new_button;
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment