Skip to content

Instantly share code, notes, and snippets.

@rniswonger
Last active March 27, 2021 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rniswonger/e537ec989d66ef2e8f78b5f55823acdd to your computer and use it in GitHub Desktop.
Save rniswonger/e537ec989d66ef2e8f78b5f55823acdd to your computer and use it in GitHub Desktop.
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