get_submit_button

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

Description

(string) get_submit_button( (string) $text = '', (string) $type = 'primary large', (string) $name = 'submit', (constant) $wrap = true, (string) $other_attributes = '' ); 

Returns (string)

Submit button HTML.

Parameters (5)

0. $text — Optional. (string) => ''
The text of the button. Default Save Changes..
1. $type — Optional. (string) => 'primary large'
The type of button. Accepts primary,, secondary., or delete. Default primary large.
2. $name — Optional. (string) => 'submit'
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.
3. $wrap — Optional. (constant) => true
True if the output button should be wrapped in a paragraph tag, false otherwise. Default true.
4. $other_attributes — Optional. (string) => ''
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 empty.

Usage

  1. if ( !function_exists( 'get_submit_button' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // Optional. The text of the button. Default 'Save Changes'. 
  5. $text = ''; 
  6.  
  7. // Optional. The type of button. Accepts 'primary', 'secondary', 
  8. // or 'delete'. Default 'primary large'. 
  9. $type = 'primary large'; 
  10. $name = 'submit'; 
  11.  
  12. // Optional. True if the output button should be wrapped in a paragraph 
  13. // tag, false otherwise. Default true. 
  14. $wrap = true; 
  15. $other_attributes = ''; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = get_submit_button($text, $type, $name, $wrap, $other_attributes); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/template.php  
  1. function get_submit_button( $text = '', $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = '' ) { 
  2. if ( ! is_array( $type ) ) 
  3. $type = explode( ' ', $type ); 
  4.  
  5. $button_shorthand = array( 'primary', 'small', 'large' ); 
  6. $classes = array( 'button' ); 
  7. foreach ( $type as $t ) { 
  8. if ( 'secondary' === $t || 'button-secondary' === $t ) 
  9. continue; 
  10. $classes[] = in_array( $t, $button_shorthand ) ? 'button-' . $t : $t; 
  11. // Remove empty items, remove duplicate items, and finally build a string. 
  12. $class = implode( ' ', array_unique( array_filter( $classes ) ) ); 
  13.  
  14. $text = $text ? $text : __( 'Save Changes' ); 
  15.  
  16. // Default the id attribute to $name unless an id was specifically provided in $other_attributes 
  17. $id = $name; 
  18. if ( is_array( $other_attributes ) && isset( $other_attributes['id'] ) ) { 
  19. $id = $other_attributes['id']; 
  20. unset( $other_attributes['id'] ); 
  21.  
  22. $attributes = ''; 
  23. if ( is_array( $other_attributes ) ) { 
  24. foreach ( $other_attributes as $attribute => $value ) { 
  25. $attributes .= $attribute . '="' . esc_attr( $value ) . '" '; // Trailing space is important 
  26. } elseif ( ! empty( $other_attributes ) ) { // Attributes provided as a string 
  27. $attributes = $other_attributes; 
  28.  
  29. // Don't output empty name and id attributes. 
  30. $name_attr = $name ? ' name="' . esc_attr( $name ) . '"' : ''; 
  31. $id_attr = $id ? ' id="' . esc_attr( $id ) . '"' : ''; 
  32.  
  33. $button = '<input type="submit"' . $name_attr . $id_attr . ' class="' . esc_attr( $class ); 
  34. $button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />'; 
  35.  
  36. if ( $wrap ) { 
  37. $button = '<p class="submit">' . $button . '</p>'; 
  38.  
  39. return $button;