Last active
August 20, 2023 14:58
-
-
Save mannieschumpert/8334811 to your computer and use it in GitHub Desktop.
Filter the submit button in Gravity Forms to change the <input type="submit> element to a <button> element. There's an example in the Gravity Forms documentation, but it lacks the proper code to show your custom button text, AND removes important attributes like the onclick that prevents multiple clicks. I was trying to solve that.
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 | |
// filter the Gravity Forms button type | |
add_filter("gform_submit_button", "form_submit_button", 10, 2); | |
function form_submit_button($button, $form){ | |
// The following line is from the Gravity Forms documentation - it doesn't include your custom button text | |
// return "<button class='button' id='gform_submit_button_{$form["id"]}'>'Submit'</button>"; | |
// This includes your custom button text: | |
return "<button class='button' id='gform_submit_button_{$form["id"]}'>{$form['button']['text']}</button>"; | |
} | |
// Oops this strips important stuff |
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 | |
// This try reveals my use case: I was actually trying to add a pseudo element to the input button | |
// This leaves the <input> as is, but prepends a span element | |
add_filter("gform_submit_button", "form_submit_button", 10, 2); | |
function form_submit_button($button, $form){ | |
return "<span></span>".$button; | |
} | |
// This kinda works for my purpose but is really not ideal |
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 | |
// This is what I settled on | |
// Change Gravity Forms submit input to a button element | |
add_filter( 'gform_submit_button', 'form_submit_button', 10, 5 ); | |
function form_submit_button ( $button, $form ){ | |
$button = str_replace( "input", "button", $button ); | |
$button = str_replace( "/", "", $button ); | |
$button .= "{$form['button']['text']}</button>"; | |
return $button; | |
} | |
// It's not perfect - it leaves some inapplicable attributes in the element | |
// but I can live with that to avoid a whole bunch more str_replace | |
// and it leaves the important onclick and tab index intact |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't believe the Gravity Forms docs still have the terrible example discussed across this six year old comment thread.