submit_button()
Displays a submit button with the specified text and classes.
Works only in the admin panel. Include the file if this function is needed on the front end:
require_once ABSPATH . '/wp-admin/includes/template.php';
Use get_submit_button() to get the result in a variable.
No Hooks.
Returns
null. Nothing. Displays the html tag <input type=submit>.
Usage
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
idargument 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 => valuewill turn into attribute="value". A string can be passed, for example:'tabindex="1"'.
Default: null
Examples
#1 Demo
submit_button( 'Apply', 'action', '', false, [ 'id' => 'doaction2' ] ); // output: // <input type="submit" id="doaction2" class="action button" value="Apply">
#2 Secondary button display
WordPress designs the main button and the additional button differently. The main button is blue and stands out more, the additional button is gray and is not as eye-catching.
By default, submit_button() outputs the main button. To display a secondary button instead, set $type = 'secondary':
submit_button( __( 'Reset', 'textdomain' ), 'secondary' );
#3 Displaying the delete button
By default, there are no styles for the delete button in WordPress. However, this may change in the future, so it's best to specify $type as 'delete' when displaying the delete button:
submit_button( __( 'Delete', 'textdomain' ), 'delete' );
By default, the delete button is styled as a secondary button, not the primary button. If you want to display it as a primary button, you can do it as follows:
submit_button( __( 'Delete', 'textdomain' ), 'delete button-primary' );
#4 Using the $name Parameter
The parameter $name can be used if you want to set the HTML attribute name for the button. The default value there is submit.
submit_button( __( 'Save Settings', 'textdomain' ), 'primary', 'wpdocs-save-settings' );
By default, $name is also used to fill the attribute id of the button. To change this, you can pass id through the parameter $other_attributes:
$other_attributes = [ 'id' => 'wpdocs-button-id' ]; submit_button( __( 'Save', 'textdomain' ), 'primary', 'wpdocs-save-settings', true, $other_attributes );
#5 Using the $wrap parameter
The $wrap parameter determines whether the button is wrapped in the <p> tag, which is the default. To disable this behavior, pass false for the fourth parameter:
submit_button( __( 'Submit', 'textdomain' ), 'primary', 'submit-form', false );
#6 Specifying other HTML attributes
You can add any HTML attributes for the button - using the $other_attributes parameter:
$other_attributes = [ 'tabindex' => '1' ]; submit_button( __( 'Go!', 'textdomain' ), 'secondary', '', true, $other_attributes );
Notes
- See: get_submit_button()
Changelog
| Since 3.1.0 | Introduced. |
submit_button() submit button code WP 7.1
function submit_button( $text = '', $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = '' ) {
echo get_submit_button( $text, $type, $name, $wrap, $other_attributes );
}