EDD_HTML_Elements

The EDD_HTML_Elements class provides a series of helper methods for rendering commonly used HTML fields with consistent markup, class names, and attributes.

The class includes methods for the following field types:

product_dropdown()

This method creates a select html element with products in it. Values are product_ids and the text is Download titles.

Example:

echo EDD()->html->product_dropdown();

This would create a drop down list of products.  The method takes one input, an array, and has these defaults:

$defaults = array(
	'name'        => 'products',
	'id'          => 'products',
	'class'       => '', 
	'multiple'    => false,
	'selected'    => 0,
	'chosen'      => false,
	'number'      => 30, 
	'bundles'     => true,
	'placeholder' => sprintf( __( 'Select a %s', 'easy-digital-downloads' ), edd_get_label_singular() )
);

Here's a description of what's in each option:

name

This would be the html "name" property of the form element.  Defaults to 'products'.

Example:

<select name="products">
id

This would be the html "id" property of the html element.  Defaults to 'products'.

Example:

<select name="products" id="products">
class

This would be the "class" property of the html element.  Defaults to nothing.

Example:

<select name="products" id="products" class="fancy">
multiple

This makes the select menu be of the "multiple" type. Defaults to false.

Example:

selected

This allows you to provide the product_id of a Download and have that one be pre-selected when the form loads.  Defaults to 0.

chosen

This activated the "chosen" jquery plugin on your select box.  Defaults to false.

Examples both closed and open:

Below is chosen with 'multiple' set to true:

number

This allows you to set a limit to the number of products in your list.  Defaults to 30.

bundles

This allows you to decide if bundles should be included in your product list.  Defaults to true.

placeholder

This is the text that goes in the first line of the select list, usually something like a title. The default is

sprintf( __( 'Select a %s', 'easy-digital-downloads' ), edd_get_label_singular() )

The above string has some plain English that says "Select a " and then looks for a string with %s.  That string is provided by the function edd_get_label_singular().  edd_get_label_singular() gets the name of your product type in singular form.  edd_get_label_singular() defaults to "Download".

customer_dropdown()

This method creates a select html element with products in it. Values are customer_ids and the text is Firstname Lastname <email@example.com>.

Example:

echo EDD()->html->customer_dropdown();

This would create a drop down list of customers.  The method takes one input, an array, and has these defaults:

$defaults = array(
    'name'        => 'customers',
    'id'          => 'customers',
    'class'       => '',  
    'multiple'    => false,
    'selected'    => 0,   
    'chosen'      => true,
    'placeholder' => __( 'Select a Customer', 'easy-digital-downloads' ),
    'number'      => 30
);

Here's a description of what's in each option:

name

This would be the html "name" property of the form element.  Defaults to 'customers'.

Example:

<select name="customers">
id

This would be the html "id" property of the html element.  Defaults to 'customers'.

Example:

<select name="customers" id="customers">
class

This would be the "class" property of the html element.  Defaults to nothing.

Example:

<select name="customers" id="customers" class="fancy">
multiple

This makes the select menu be of the "multiple" type. Defaults to false.

Example:

selected

This allows you to provide a customer_id and have that one be pre-selected when the form loads.  Defaults to 0.

chosen

This activated the "chosen" jquery plugin on your select box.  Defaults to false.

Examples both closed and open:

number

This allows you to set a limit to the number of customers in your list.  Defaults to 30.

placeholder

This is the text that goes in the first line of the select list, usually something like a title. The default is "Select a Customer".

discount_dropdown()

This method creates a select html element with a list of Discounts in it. Values are discount_ids and the text is the title of the Discount.

Example:

echo EDD()->html->discount_dropdown();

This would create a drop down list of discounts.

Input

This method takes three values as input.

name

This is the name of the html element.  The default is 'edd_discounts'

selected

This is the item that should be selected when the select box loads.  The default is to have nothing set to be selected, and the default item will be the first discount on the list.

status

Discounts can be active or inactive.  The 'status' option allows a choice of either one.  The default is to not declare, and all Discounts are loaded.

category_dropdown()

This method creates a select html element with a list of Download categories in it. Values are term_ids and the text is the title of the category.

Example:

echo EDD()->html->category_dropdown();

This would create a drop down list of Download categories.

Input

This method takes two values as input.

name

This is the name of the html element.  The default is 'edd_categories'

selected

This is the item that should be selected when the select box loads. The default is to have nothing set to be selected, and the select box will have an option titled "All Categories".

year_dropdown()

This method creates a select html element with a list of years in it. Text and option value are both a 4 digit year.

Example:

echo EDD()->html->year_dropdown();

This would create a drop down list of 6 years, current year selected by default.

Input

This method takes two values as input.

name

This is the name of the html element.  The default is 'year'

selected

This is the item that should be selected when the select box loads.  Requires a four digit integer.

month_dropdown()

This method creates a select html element with a list of months in it. Values are unpadded integers and text is three letter abbreviations of months.

Example:

echo EDD()->html->year_dropdown();

This would create a drop down list of 12 months, current month selected by default.

Input

This method takes two values as input.

name

This is the name of the html element.  The default is 'month'

selected

This is the item that should be selected when the select box loads. Requires a two digit integer.

select()

This method creates a select html element and allows you to populate it with any single dimensional array.

Example:

echo EDD()->html->select( $args );

The method takes one input, an array, and has these defaults:

$args = array(
    'options'          => array(),
    'name'             => null,
    'class'            => '',
    'id'               => '',
    'selected'         => 0,
    'chosen'           => false,
    'placeholder'      => null,
    'multiple'         => false,
    'show_option_all'  => _x( 'All', 'all dropdown items', 'easy-digital-downloads' ),
    'show_option_none' => _x( 'None', 'no dropdown items', 'easy-digital-downloads' ),
);

Here's a description of what's in each option:

options

This is a single dimensional array of things like this:

$args['options'] = array(
    '1' => 'Thing 1',
    '2' => 'Thing 2',
    '3' => 'Thing 3',
    '4' => 'Thing 4',
);

The array keys are used as the select option values, and the array values are used as the select option text.

name

This would be the html "name" property of the form element.  Defaults to null, required to make a useful form element.

id

This would be the html "id" property of the html element.  Defaults to empty.

class

This would be the "class" property of the html element.  Defaults to empty.

multiple

This makes the select menu be of the "multiple" type. Defaults to false.

Example:

selected

This allows you to provide a key from your array and have that one be pre-selected when the form loads.  Defaults to 0.

chosen

This activated the "chosen" jquery plugin on your select box.  Defaults to false.

Examples both closed and open:

placeholder

This is the text that goes in the first line of the select list, usually something like a title. The default is null.

show_option_all

This creates a select option with a value of "all", and is inserted into the argument list this way:

'show_option_all'  => _x( 'All', 'all dropdown items', 'easy-digital-downloads' ),
show_option_none

This creates a select option with a value of "-1", and is inserted into the argument list this way:

'show_option_all'  => _x( 'None', 'no dropdown items', 'easy-digital-downloads' ),

checkbox()

This method creates a single checkbox html element.

Note: This function does not create any wrapping HTML like <label>.

Example:

<label><?php echo EDD()->html->checkbox( $args ); ?> Pick me!</label>

The method takes one input, an array, and has these defaults:

$args = array(
    'name'     => null,
    'current'  => null,
    'class'    => 'edd-checkbox',
    'options'  => array(
        'disabled' => false,
        'readonly' => false
    )   
);

Here's a description of what's in each option:

name

This would be the html "name" property of the form element.  Defaults to null, required to make a useful form element.

Note: the name is also used for the 'id'.

current

This is the pre-existing value of the field. It's used to determine if the checkbox should default to checked.

class

This is the value of the 'class' html element. Defaults to 'edd-checkbox'.

options

This needs to be an array, and can hold two options.

  • disabled - Sets the field to disabled or not with true or false.
  • readonly - Sets the field to readonly or not with true or false.

text()

This method creates a plain text html element.

Example:

<?php echo EDD()->html->text( $args ); ?>

The method takes one input, an array, and has these defaults:

$args = array(
    'id'           => '',  
    'name'         => 'text', 
    'value'        => NULL, 
    'label'        => NULL, 
    'desc'         => NULL, 
    'placeholder'  => '',  
    'class'        => 'regular-text',
    'disabled'     => false,
    'autocomplete' => '',
    'data'         => false
);

Here's a description of what's in each option:

id

This would be the html "id" property of the html element.  Defaults to empty.

name

This would be the html "name" property of the form element.  Defaults to "text".

value

This is the pre-existing value of the field. It's used to pre-fill the text field with this value. Defaults to NULL.

label

This is the text wrapped in a <label> tag.  The <label> tag also has a class of "edd-label" and a "for" element which uses the id mentioned above. Defaults to NULL.

desc

This is text that is wrapped in a span with a class of "edd-description".  It's placed between the label and the input.  Defaults to NULL.

placeholder

This text is placed in the "placeholder" element of the <input>.  Rendering depends on the browser.   You can read more about the placeholder element at w3schools.  Default is empty.

class

This is the value of the 'class' html element. Defaults to 'regular-text'.

disabled

This sets the field to disabled or not.  Boolean, defaults to false.

autocomplete

This sets the autocomplete element to "on" or "off".  Defaults to empty, which equates to on in most modern browsers.

data

This accepts an array, and creates data html elements from the array. Example:

array(
	'price' => '42.00',
	'variable-price' => 'no',
);

The above array would result in this html being places inside the input:

data-price="42.00" data-variable-price="no"

Defaults to false.

textarea()

This creates a standard multi-line HTML textarea.

Example:

<?php echo EDD()->html->textarea( $args ); ?>

The method takes one input, an array, and has these defaults:

$args = array(
    'name'        => 'textarea',
    'value'       => null,
    'label'       => null,
    'desc'        => null,
    'class'       => 'large-text',
    'disabled'    => false
);<br>

Here's a description of what's in each option.

name

This would be the html "name" property of the form element.  Defaults to "textarea".

value

This is the pre-existing value of the field. It's used to pre-fill the text field with this value. Defaults to NULL.

label

This is the text wrapped in a <label> tag.  The <label> tag also has a class of "edd-label" and a "for" element which uses the id mentioned above. Defaults to NULL.

desc

This is text that is wrapped in a span with a class of "edd-description".  It's placed after the textarea tag.  Defaults to NULL.

class

This is the value of the 'class' html element. Defaults to 'large-text'.

disabled

This sets the field to disabled or not.  Boolean, defaults to false.

ajax_user_search()

This method creates a text field for searching for EDD users.  It uses ajax to search as you type, and allows you to click to choose a result to populate the field.

Note: this field only works on EDD admin pages.  Not the front of WordPress, and not other WordPress admin areas.

Example:

<?php echo EDD()->html->ajax_user_search( $args ); ?>

The method takes one input, an array, and has these defaults:

$args = array(
    'name'        => 'user_id',
    'value'       => NULL,
    'placeholder' => __( 'Enter username', 'easy-digital-downloads' ),
    'label'       => NULL,
    'class'       => '',  
    'disabled'    => false,
    'autocomplete'=> 'off',
);

Here's a description of what's in each option.

name

This would be the html "name" property of the form element.  Defaults to "user_id".

value

This is the pre-existing value of the field. It's used to pre-fill the text field with this value. Defaults to NULL.

placeholder

This text gets rendered in the input box in most browsers. Defaults to:

__( 'Enter username', 'easy-digital-downloads' )
label

This text does not appear on the front of the site, but is used with aria for screen readers. Defaults to NULL.

class

This is the value of the 'class' html element. Defaults to empty.

disabled

This sets the field to disabled or not.  Boolean, defaults to false.

autocomplete

This sets the autocomplete element to "on" or "off".  Defaults to empty, which equates to on in most modern browsers.