get_submit_button

函式
get_submit_button ( $text = '', $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = '' )
引數
  • (string) $text Optional. The text of the button. Default 'Save Changes'.
    Required:
    Default: (empty)
  • (string) $type Optional. The type and CSS class(es) of the button. Core values include 'primary', 'small', and 'large'. Default 'primary large'.
    Required:
    Default: 'primary large'
  • (string) $name 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'.
    Required:
    Default: 'submit'
  • (bool) $wrap Optional. True if the output button should be wrapped in a paragraph tag, false otherwise. Default true.
    Required:
    Default: true
  • (array|string) $other_attributes 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 empty.
    Required:
    Default: (empty)
返回值
  • (string) Submit button HTML.
定義位置
相關方法
submit_buttonget_site_option_media_buttonget_post_customget_site_url
引入
3.1.0
棄用
-

get_submit_button: 這個函式生成並返回一個提交按鈕的HTML程式碼。它需要三個引數:要顯示在按鈕上的文字,一個可選的按鈕名稱,以及一個可選的包含在按鈕元素中的屬性陣列。

返回一個提交按鈕,並提供文字和適當的類別。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >