get_submit_button()WP 3.1.0

Returns a submit button, with provided text and appropriate class

Used By: submit_button()
1 time — 0.000051 sec (very fast) | 50000 times — 1.38 sec (fast)

No Hooks.

Return

String. Submit button HTML.

Usage

get_submit_button( $text, $type, $name, $wrap, $other_attributes );
$text(string)
The text of the button.
Default: 'Save Changes'
$type(string)
The type and CSS class(es) of the button. Core values include 'primary', 'small', and 'large'.
Default: 'primary large'
$name(string)
The HTML name of the submit button. If no id attribute is given in the $other_attributes parameter, $name will be used as the button's id.
Default: 'submit'
$wrap(true|false)
True if the output button should be wrapped in a paragraph tag, false otherwise.
Default: true
$other_attributes(array|string)
Other attributes that should be output with the button, mapping attributes to their values, e.g. array('id'=>'search-submit'). These key/value attribute pairs will be output as attribute="value", where attribute is the key. Attributes can also be provided as a string, e.g. id="search-submit", though the array format is generally preferred.
Default: empty string

Examples

0

#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">

Changelog

Since 3.1.0 Introduced.

get_submit_button() code WP 6.5.2

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;
}