Last active
March 27, 2021 19:03
-
-
Save rniswonger/e537ec989d66ef2e8f78b5f55823acdd to your computer and use it in GitHub Desktop.
Gravity Forms: Replace the submit <input> with a <button>
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
/** | |
* 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