Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active April 29, 2022 18:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save richardW8k/8370923 to your computer and use it in GitHub Desktop.
Save richardW8k/8370923 to your computer and use it in GitHub Desktop.
convert the next, previous and/or submit button <input> to <button> and uses the input value to create the button text inside a <span>, this means you can set the button text by editing the form or page break settings.
<?php
/**
* Filters the next, previous and submit buttons.
* Replaces the forms <input> buttons with <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.
*/
add_filter( 'gform_next_button', 'input_to_button', 10, 2 );
add_filter( 'gform_previous_button', 'input_to_button', 10, 2 );
add_filter( 'gform_submit_button', 'input_to_button', 10, 2 );
function input_to_button( $button, $form ) {
$dom = new DOMDocument();
$dom->loadHTML( $button );
$input = $dom->getElementsByTagName( 'input' )->item(0);
$new_button = $dom->createElement( 'button' );
$button_span = $dom->createElement( 'span', $input->getAttribute( 'value' ) );
$new_button->appendChild( $button_span );
$input->removeAttribute( 'value' );
foreach( $input->attributes as $attribute ) {
$new_button->setAttribute( $attribute->name, $attribute->value );
}
$input->parentNode->replaceChild( $new_button, $input );
return $dom->saveHtml( $new_button );
}
@vanbernaert
Copy link

Hi, I just found your awesome idea to replace input buttons by

copy/pasted code into functions.php ... no errors ... but I have just no buttons anymore now.
But I suppose I need to execute the function 'input_to_button' to get this working, but how?

Thx a lot!

@richardW8k
Copy link
Author

If the add_filter lines are there the function will run. What version of PHP are you running, the ability to include a parameter in the saveHTML call was only introduced in PHP 5.3.6.

@vanbernaert
Copy link

works like a charm after PHP 5.3.3 update to 5.4
thx a lot!

@lkraav
Copy link

lkraav commented Feb 19, 2015

Using DOMDocument and everything. You sir, have done very well here. I don't have to adjust a single thing :)

@lkraav
Copy link

lkraav commented Feb 19, 2015

Maybe one thing: why add the span granularity? Why not have input "value" attribute simply as button text node?

$new_button->appendChild( $dom->createTextNode( $input->getAttribute( "value" ) ) );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment