get_submit_button()WP 3.1.0

Получает HTML код кнопки submit с указанным текстом, классами и атрибутами.

Чтобы сразу вывести результат на экран используйте submit_button().

Работает только в админ-панели.

Основа для: submit_button()
1 раз — 0.000051 сек (очень быстро) | 50000 раз — 1.38 сек (быстро)

Хуков нет.

Возвращает

Строку. HTML код тега <input type=submit>.

Использование

$submit = get_submit_button( $text, $type, $name, $wrap, $other_attributes );
$text(строка)
Текст кнопки.
По умолчанию: null (__('Save Changes'))
$type(строка)
Тип и CSS класс кнопки. Может быть: 'primary', 'secondary', 'delete' или произвольный класс.
По умолчанию: 'primary'
$name(строка)
Атрибут name кнопки submit.Если в аргументе id следующего параметра $other_attributes ничего не указано, то id будет равен $name.
По умолчанию: 'submit'
$wrap(логический)
Ставим true, если кнопку нужно обернуть в тег <p>.
По умолчанию: true
$other_attributes(массив/строка)
Массив устанавливающий атрибуты тега input по такому шаблону: attribute => value превратиться в attribute="value". Можно передать строку например: 'tabindex="1"'.
По умолчанию: null

Примеры

0

#1 Добавим кнопку сабмита в текущий HTML код

echo get_submit_button( 'Применить', 'action', '', false, [ 'id' => 'doaction2' ] );

// выведет:
// <input type="submit" id="doaction2" class="button action" value="Применить">

Список изменений

С версии 3.1.0 Введена.

Код get_submit_button() WP 6.5.2

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