Using WordPress ‘get_submit_button()’ PHP function

The get_submit_button() WordPress PHP function returns a submit button with the provided text and appropriate class.

Usage

echo get_submit_button('Click Me', 'primary large', 'my_button', true, array('tabindex' => '1'));

Parameters

  • $text (string) Optional: The text of the button. Default ‘Save Changes’.
  • $type (string) Optional: The type and CSS class(es) of the button. Core values include ‘primary’, ‘small’, and ‘large’. Default ‘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 ‘submit’.
  • $wrap (bool) Optional: True if the output button should be wrapped in a paragraph tag, false otherwise. Default: 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: ”.

More information

See WordPress Developer Resources: get_submit_button

Note: This function is not designed to be run outside wp-admin. Running the function outside wp-admin results in a Fatal error: Uncaught Error: Call to undefined function get_submit_button().

Examples

Default Button

Create a default submit button with “Save Changes” text.

echo get_submit_button();

Custom Button Text and Classes

Create a button with custom text and classes.

echo get_submit_button('Submit Form', 'primary small', 'custom_submit');

Button without Paragraph Tag

Create a button without wrapping it in a paragraph tag.

echo get_submit_button('Update', 'primary large', 'update_button', false);

Button with Additional Attributes

Create a button with a custom tabindex and data attribute.

echo get_submit_button('Save', 'primary', 'save_button', true, array('tabindex' => '1', 'data-custom' => 'example'));

Button with String Attributes

Create a button with attributes provided as a string.

echo get_submit_button('Confirm', 'large', 'confirm_button', true, 'tabindex="2" data-action="confirm"');