get_submit_button() WordPress Function

The get_submit_button() function is used to display a submit button for a form. This function takes a number of arguments to determine the type and appearance of the button.

get_submit_button( string $text = '', string $type = 'primary large', string $name = 'submit', bool $wrap = true, array|string $other_attributes = '' ) #

Returns a submit button, with provided text and appropriate class.


Parameters

$text

(string)(Optional) The text of the button. Default 'Save Changes'.

Default value: ''

$type

(string)(Optional) The type and CSS class(es) of the button. Core values include 'primary', 'small', and 'large'.

Default value: 'primary large'

$name

(string)(Optional) The HTML name of the submit button. Defaults to "submit". If no id attribute is given in $other_attributes below, $name will be used as the button's id.

Default value: 'submit'

$wrap

(bool)(Optional) True if the output button should be wrapped in a paragraph tag, false otherwise.

Default value: true

$other_attributes

(array|string)(Optional) Other attributes that should be output with the button, mapping attributes to their values, such as array( 'tabindex' => '1' ). These attributes will be output as attribute="value", such as tabindex="1". Other attributes can also be provided as a string such as tabindex="1", though the array format is typically cleaner.

Default value: ''


Top ↑

Return

(string) Submit button HTML.


Top ↑

More Information

  • $type can be a single value, or a space separated list of values, or an array of values. The values determine the HTML classes of the button.
    • If $type is ‘delete’, the classes are ‘button delete’.
    • Otherwise the first class is ‘button’, followed by any of these in order of appearance:
      • type value ‘primary’ makes class ‘button-primary’
      • type value ‘small’ makes class ‘button-small’
      • type value ‘large’ makes class ‘button-large’
      • type value ‘secondary’ or ‘button-secondary’ is ignored (the ‘button’ class has the styling)
      • any other type value ‘foo’ makes the class ‘foo’
    • For example, the default $type ‘primary large’ results in a button with HTML classes ‘button button-primary button-large’.
  • The related function submit_button() echos the button instead of returning it as a string. It has a different default $type, ‘primary’, resulting in the HTML classes ‘button button-primary’.

Top ↑

Source

File: wp-admin/includes/template.php

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


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.