Input
Methods¶
contains()¶
Check if the collection contains a key / value pair
1 | contains ( $key = null, $value = null ) |
Parameter | Type | Default | Description |
---|---|---|---|
$key |
string | null |
Key |
$value |
string | null |
Value to look for |
@return bool |
Examples
Basic:
1 2 3 4 5 6 | <?php $input = $form->input(); $input->contains( 'email', 'foo@bar.com'); // true $input->contains( 'email', 'bar@foo.com'); // false |
Conditional:
1 2 3 4 5 6 7 | <?php $input = $form->input(); if ( $input->contains( 'email', 'foo@bar.com') ) { // ...do something } |
display()¶
Displays (echo) an encoded input value.
If you need to assign the value, use escape() instead.
1 | display( $field = null, callable $callback = null ) |
Parameter | Type | Default | Description |
---|---|---|---|
$field |
string | null |
|
$callback |
callable | esc_html() |
|
@return string|null |
Examples
Basic:
1 2 3 | <?php // eg. user entered <h1>Bar</h1> $form->display('name'); |
Note
The default callback is esc_html() when none is provided.
Callback:
1 2 3 | <?php // eg. user entered <h1>Bar</h1> $form->display('name', 'strip_tags'); // Bar |
Tip
The callable can be a string reference to a function or a closure.
escape()¶
Assign an escaped input value.
1 | escape( $key, callable $callback = null ) |
Parameter | Type | Default | Description |
---|---|---|---|
$key |
string | Key containing the string | |
$callback |
callable | esc_html() |
Callback to pass string |
@return string|null |
Examples
Basic:
1 2 3 4 | <?php // eg. user entered <h1>Bar</h1> // no echo $name = $form->input()->escape('name'); // <h1>Bar</h1> |
Callback:
1 2 3 | <?php // eg. user entered <h1>Bar</h1> $name = $form->input()->escape('name', 'strip_tags'); // Bar |
Note
Make sure to use a callable that is appropriate to the output context.
Custom callback:
1 2 3 4 5 6 7 8 9 | <?php // over-engineered string concatenation function add_lorem( $string ) { return $string .'_lorem'; } $new_name = $form->input()->escape('name', 'add_lorem'); // Bar_lorem |
Closure:
1 2 3 4 5 6 7 | <?php $new_name = $form->input()->escape('name', function( $string ){ return $string .'_lorem'; }); // Bar_lorem |
Callback with multiple parameters:
1 2 3 4 5 6 7 8 9 10 11 | <?php // even more over-engineered string concatenation function wfv_example( $value, $arg2, $arg3 ) { return $arg2 .'-'. $value .'-'. $arg3; } $callback = array( 'wfv_example', array( 'second', 'third' ) ); $new_email = $form->input()->escape( 'email', $callback ); // second-foo@bar.com-third |
has()¶
Check if the collection has a given key
1 | has ( $key = null ) |
Parameter | Type | Default | Description |
---|---|---|---|
$key |
string | null |
Key to check existence |
@return bool |
Examples
Basic:
1 2 3 | <?php $form->input()->has('email'); // true |
Conditional:
1 2 3 4 5 6 7 | <?php $input = $form->input(); if ( $input->has('email') ) { // ...do something } |
is_populated()¶
Checks if the collection has data.
1 | is_populated() |
@return bool