woocommerce_wp_text_input()
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 theidattribute of theinputtag. If not specified, you will get a PHP errorPHP Notice: Undefined index: id in ....
Also used to get the values of thevalueandnameattributes of theinputtag, for theforattribute of thelabeltag, for an additional css class for the container in the form of aptag. -
label(string) (required)
Content of thelabeltag. When output, it is sanitized by the function wp_kses_post(). If not specified, you will get a PHP errorPHP Notice: Undefined index: label in .... -
class(string)
Value of theclassattribute of theinputtag.
Default: short -
placeholder(string)
Value of theplaceholderattribute of theinputtag. When output, it is sanitized by the function esc_attr().
Default: '' -
style(string)
Value of thestyleattribute of theinputtag. When output, it is sanitized by the function esc_attr().
Default: '' -
wrapper_class(string)
Value of theclassattribute of theptag (container). When output, it is sanitized by the function esc_attr(). Theptag will also have a static classform-fieldand a dynamic class{parameter id}_field.
Default: value of the id parameter -
value(string)
Value of thevalueattribute of theinputtag. When output, it is sanitized by the function esc_attr().
Default: value of the id parameter -
name(string)
Value of thenameattribute of theinputtag. When output, it is sanitized by the function esc_attr(), as well as other functions, depending on thedata_typeparameter.
Default: get_post_meta( $thepostid, $field['id'], true ) -
type(string)
Value of thetypeattribute of theinputtag. 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_pricewill be added to theclassparameter. - decimal - the value will be processed by the function wc_format_localized_decimal(), and the value
wc_input_decimalwill be added to theclassparameter. - stock - the value will be processed by the function wc_stock_amount(), and the value
wc_input_stockwill be added to theclassparameter. - url - the value will be processed by the function esc_url(), and the value
wc_input_urlwill be added to theclassparameter.
Default: ''
- price - the value will be processed by the function wc_format_localized_price(), and the value
-
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_tipparameter is not specified. -
desc_tip(boolean)
If anything other thanfalseis 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
#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>
