woocommerce_wp_text_input()WC 1.0

Outputs a text field.

Used in WooCommerce meta boxes, so for the function to work correctly, global variables $thepostid and $post are needed, otherwise you may get a PHP error Trying to get property 'ID' of non-object in ...

No Hooks.

Returns

null. Outputs HTML.

Usage

woocommerce_wp_text_input( $field, ?WC_Data $data );
$field(array) (required)

An array of parameters (configuration) for the field. Accepts the following values:

  • id(string) (required)
    Value of the id attribute of the input tag. If not specified, you will get a PHP error PHP Notice: Undefined index: id in ....
    Also used to get the values of the value and name attributes of the input tag, for the for attribute of the label tag, for an additional css class for the container in the form of a p tag.

  • label(string) (required)
    Content of the label tag. When output, it is sanitized by the function wp_kses_post(). If not specified, you will get a PHP error PHP Notice: Undefined index: label in ....

  • class(string)
    Value of the class attribute of the input tag.
    Default: short

  • placeholder(string)
    Value of the placeholder attribute of the input tag. When output, it is sanitized by the function esc_attr().
    Default: ''

  • style(string)
    Value of the style attribute of the input tag. When output, it is sanitized by the function esc_attr().
    Default: ''

  • wrapper_class(string)
    Value of the class attribute of the p tag (container). When output, it is sanitized by the function esc_attr(). The p tag will also have a static class form-field and a dynamic class {parameter id}_field.
    Default: value of the id parameter

  • value(string)
    Value of the value attribute of the input tag. When output, it is sanitized by the function esc_attr().
    Default: value of the id parameter

  • name(string)
    Value of the name attribute of the input tag. When output, it is sanitized by the function esc_attr(), as well as other functions, depending on the data_type parameter.
    Default: get_post_meta( $thepostid, $field['id'], true )

  • type(string)
    Value of the type attribute of the input tag. List of possible type values. When output, it is sanitized by the function esc_attr().
    Default: 'text'

  • data_type(string)
    Data type for output. Based on this parameter, a special sanitization function will be applied. In any case, the data will be additionally processed by the function esc_attr(). Can be:

    • price - the value will be processed by the function wc_format_localized_price(), and the value wc_input_price will be added to the class parameter.
    • decimal - the value will be processed by the function wc_format_localized_decimal(), and the value wc_input_decimal will be added to the class parameter.
    • stock - the value will be processed by the function wc_stock_amount(), and the value wc_input_stock will be added to the class parameter.
    • url - the value will be processed by the function esc_url(), and the value wc_input_url will be added to the class parameter.

    Default: ''

  • description(string)
    Description of the field. Output below the input field as

    <span class="description">value</span>

    When output, it is sanitized by the function wp_kses_post(). The description will be shown (it will be visible) if the desc_tip parameter is not specified.

  • desc_tip(boolean)
    If anything other than false is specified, the field description will be output as a tooltip (a question mark icon, hovering over which will show the description). Output as

    <span class="woocommerce-help-tip" data-tip="value description"></span>

    Default: false

  • custom_attributes(array)
    Arbitrary HTML attributes in the form of an array with pairs [ attribute => value ].

Examples

0

#1 Display WooCommerce price field

This example is taken for the "Base Price" field from the code of the WooCommerce plugin itself.

woocommerce_wp_text_input(
	array(
		'id'        => '_regular_price',
		'value'     => $product_object->get_regular_price( 'edit' ),
		'label'     => __( 'Regular price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
		'data_type' => 'price',
	)
);

Get HTML:

<p class="form-field _regular_price_field ">

	<label for="_regular_price">
		Base price (£)
	</label>

	<input  type="text"
			class="short wc_input_price"
			style=""
			name="_regular_price"
			id="_regular_price"
			value=""
			placeholder=""
	/>

</p>

woocommerce_wp_text_input() code WC 10.3.5

function woocommerce_wp_text_input( $field, ?WC_Data $data = null ) {
	global $post;

	$field['placeholder']   = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'short';
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
	$field['value']         = $field['value'] ?? OrderUtil::get_post_or_object_meta( $post, $data, $field['id'], true );
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
	$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
	$field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
	$data_type              = empty( $field['data_type'] ) ? '' : $field['data_type'];

	switch ( $data_type ) {
		case 'price':
			$field['class'] .= ' wc_input_price';
			$field['value']  = wc_format_localized_price( $field['value'] );
			break;
		case 'decimal':
			$field['class'] .= ' wc_input_decimal';
			$field['value']  = wc_format_localized_decimal( $field['value'] );
			break;
		case 'stock':
			$field['class'] .= ' wc_input_stock';
			$field['value']  = wc_stock_amount( $field['value'] );
			break;
		case 'url':
			$field['class'] .= ' wc_input_url';
			$field['value']  = esc_url( $field['value'] );
			break;

		default:
			break;
	}

	// Custom attribute handling
	$custom_attributes = array();

	if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {

		foreach ( $field['custom_attributes'] as $attribute => $value ) {
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
		}
	}

	echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
		<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';

	$help_tip    = null;
	$description = null;
	if ( ! empty( $field['description'] ) ) {
		if ( is_array( $field['description'] ) ) {
			$help_tip    = reset( $field['description'] );
			$description = end( $field['description'] );
		} elseif ( false !== $field['desc_tip'] ) {
			$help_tip = $field['description'];
		} else {
			$description = $field['description'];
		}
	}

	if ( ! is_null( $help_tip ) ) {
		echo wc_help_tip( $help_tip );
	}

	echo '<input type="' . esc_attr( $field['type'] ) . '" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( ' ', $custom_attributes ) . ' /> ';

	if ( ! is_null( $description ) ) {
		$hidden_class = true === ( $field['description_hidden'] ?? false ) ? ' hidden' : '';
		//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '<span class="description' . $hidden_class . '">' . wp_kses_post( $description ) . '</span>';
	}

	echo '</p>';
}