get_submit_button() │ WP 3.1.0
Gets the HTML code for the submit button with the specified text, classes, and attributes.
To immediately display the result on the screen, use submit_button() .
Works only in the admin panel.
1 time — 0.000051 sec (very fast) | 50000 times — 1.38 sec (fast)
No Hooks.
Returns
String. HTML code for the tag <input type=submit> .
Usage
$submit = get_submit_button( $text, $type, $name, $wrap, $other_attributes );
$text(string)
Text of the button.
Default: null (__('Save Changes') )
$type(string)
Type and CSS class of the button. Can be: 'primary', 'secondary', 'delete', or a custom class.
Default: 'primary'
$name(string)
Name attribute of the submit button. If nothing is specified in the id argument of the next parameter $other_attributes , then id will equal $name .
Default: 'submit'
$wrap(boolean)
Set to true if the button needs to be wrapped in a <p> tag.
Default: true
$other_attributes(array/string)
Array setting the attributes of the input tag in the format: attribute => value will turn into attribute="value". You can pass a string, for example: 'tabindex="1"'.
Default: null
Examples
#1 Add a submit button into the HTML code
echo get_submit_button( 'Apply', 'action', '', false, [ 'id' => 'doaction2' ] );
// output:
// <input type="submit" id="doaction2" class="action button" value="Apply">
Add Your Own Example
Changelog
get_submit_button() get submit button code
WP 6.8.3
function get_submit_button( $text = '', $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = '' ) {
if ( ! is_array( $type ) ) {
$type = explode( ' ', $type );
}
$button_shorthand = array( 'primary', 'small', 'large' );
$classes = array( 'button' );
foreach ( $type as $t ) {
if ( 'secondary' === $t || 'button-secondary' === $t ) {
continue;
}
$classes[] = in_array( $t, $button_shorthand, true ) ? 'button-' . $t : $t;
}
// Remove empty items, remove duplicate items, and finally build a string.
$class = implode( ' ', array_unique( array_filter( $classes ) ) );
$text = $text ? $text : __( 'Save Changes' );
// Default the id attribute to $name unless an id was specifically provided in $other_attributes.
$id = $name;
if ( is_array( $other_attributes ) && isset( $other_attributes['id'] ) ) {
$id = $other_attributes['id'];
unset( $other_attributes['id'] );
}
$attributes = '';
if ( is_array( $other_attributes ) ) {
foreach ( $other_attributes as $attribute => $value ) {
$attributes .= $attribute . '="' . esc_attr( $value ) . '" '; // Trailing space is important.
}
} elseif ( ! empty( $other_attributes ) ) { // Attributes provided as a string.
$attributes = $other_attributes;
}
// Don't output empty name and id attributes.
$name_attr = $name ? ' name="' . esc_attr( $name ) . '"' : '';
$id_attr = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$button = '<input type="submit"' . $name_attr . $id_attr . ' class="' . esc_attr( $class );
$button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />';
if ( $wrap ) {
$button = '<p class="submit">' . $button . '</p>';
}
return $button;
}
Related Functions