get()
Return an entry by key
Parameters
$field | required | Field name |
$default | optional, default is "" | Default value when field doesn't exist |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith']
$first = Field::get('first');
// John
$occupation = Field::get('occupation');
// ""
Using a default value
// ['first' => 'John', 'last' => 'Smith']
$occupation = Field::get('occupation', 'Unemployed');
// Unemployed
display()
Display an entry by key
Parameters
$field | required | Field name |
$default | optional, default is "" | Default value when field doesn't exist |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith']
Field::display('first');
// prints "John"
Field::display('occupation');
// prints ""
Using a default value
// ['first' => 'John', 'last' => 'Smith']
Field::display('occupation', 'Unemployed');
// prints "Unemployed"
hasKey()
Check existance of a key
Parameters
$field | required | Field name |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith', 'occupation' => '']
Field::hasKey('first');
// true
Field::hasKey('occupation');
// true
exists()
Check existance of a key and it's contents
Parameters
$field | required | Field name |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith', 'occupation' => '']
$first = Field::exists('first');
// true
$occupation = Field::exists('occupation');
// false
html()
Display a field wrapped in HTML. Field contents are referenced by %s
.
Parameters
$field | required | Field name |
$wrap | optional, default is %s | Formatted HTML |
$default | optional, default is "" | Default value when field doesn't exist |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith']
Field::html('first', '<p>%s</p>');
// prints "<p>John</p>"
Using a default value
// ['first' => 'John', 'last' => 'Smith']
Field::html('occupation', '<p>%s</p>', '<p>Developer</p>');
// prints "<p>Developer</p>"
If you only fill out the first field this method operates exactly like Field::display()
// ['first' => 'John', 'last' => 'Smith']
Field::html('first');
// prints "John"
equals()
Compare a value against a field
Parameters
$field | required | Field name |
$val | required | Value to compare against |
Examples
Basic usage
// ['first' => 'John', 'last' => 'Smith']
$first = Field::equals('first', 'John');
// true
image()
Display an <img>
. The alt attribute is automatically filled out based on what is set within WordPress. If you need full control over the output of the image tag use Field::src()
Note: This method assumes the return value of the image field is an ID.
Parameters
$field | required | Field key |
$size | optional, default is full | The size of the returned image. All standard WordPress image sizes are recognized. The size can also be represented as an array. |
$attrs | optional, default is [] | Additional attributes that should be added to the <img> tag |
$crop | optional, default is false | Setting this value will crop the output image into the dimensions provided in the $size parameter |
Examples
Basic usage
// ['image' => 10]
Field::image('image');
// prints <img src="/path/to/image" alt="Image alt">
Field::image('image', 'medium');
// prints <img src="/path/to/medium-image" alt="Image alt">
Setting additional attributes
// ['image' => 10]
Field::image('image', 'full', ['class' => 'accent']);
// prints <img src="/path/to/image" alt="Image alt" class="accent">
When using the $crop
parameter an image with the set dimensions is created in the file system removing the need for this method to crop with every use. There is a Field::croppedImage()
shorthand the removes the need to set all parameters.
// ['image' => 10]
Field::image('image', [500, 500], ['class' => 'accent'], true);
// prints <img src="/path/to/500x500image" alt="Image alt" class="accent">;
url()
Adds http if the provided url is missing a protocol. On page and protocol relative urls are ignored.
Parameters
$field | required | Field name |
Examples
Basic usage
// ['link1' => 'google.com', 'link2' => '#relative']
Field::url('link1');
// http://google.com
Field::url('link2');
// #relative
shortcode()
Wraps field contents with do_shortcode
and prints to screen.
Parameters
$field | required | Field name |
Examples
Basic usage
// ['shortcode' => '[display_content]']
Field::shortcode('shortcode');
// Prints output of [display_content]
croppedImage()
This is a shorthand version of Field::image()
with the crop parameter set. If you don't set the size in an array this method works exactly the same as Field::image()
.
Note: This method assumes the return value of the image field is an ID.
Parameters
$field | required | Field key |
$size | optional, default is full | The size of the returned image. It is recommended you provide the size in an array, however all standard WordPress image sizes are recognized. |
$attrs | optional, default is [] | Additional attributes that should be added to the <img> tag |
Examples
Basic usage
// ['image' => 10]
Field::croppedImage('image', [500, 500]);
// prints <img src="/path/to/500x500-image" alt="Image alt">
Setting additional attributes
// ['image' => 10]
Field::croppedImage('image', [500, 500], ['class' => 'accent']);
// prints <img src="/path/to/500x500-image" alt="Image alt" class="accent">
src()
Provides the path to a resized image. If the image size requested doesn't exist it will automatically be created before returning.
Note: This method assumes the return value of the image field is an attachment ID.
Parameters
$field | required | Field key |
$size | optional, default is full | The size of the returned image. It is recommended you provide the size in an array, however all standard WordPress image sizes are recognized. |
$crop | optional, default is false | Setting this value will crop the output image into the dimensions provided in the $size parameter |
Examples
Basic usage
// ['image' => 10]
echo '<img src="' . Field::src('image', [500, 500]) . '">
// prints <img src="/path/to/500x500-image">
echo 'style="background-image: url(' . Field::src('image', [500, 500]) . ');"
// prints style="background-image: url(/path/to/500x500-image);"
setData()
Store data that all other methods reference. Existing data is stored internally and can be restored by calling Field::restore()
.
Parameters
$data | required | Provide an array that should be stored for future use |
Examples
Basic usage
$data = ['first' => 'John', 'last' => 'Smith']
Field::setData($data);
$first = Field::get('first');
// John
restore()
Reset data to the previous set.
Parameters
None |
Examples
Basic usage
Field::setData(['example' => 'Tom']);
Field::setData(['example' => 'Bill']);
$first = Field::get('example');
// Bill
Field::restore();
$first = Field::get('example');
// Tom
getAll()
Return current data set
Parameters
None |
Examples
Basic usage
Field::setData(['first' => 'Jim', 'last' => 'Jones']);
$data = Field::getAll();
// ['first' => 'Jim', 'last' => 'Jones']
debug()
Displays current data set wrapped in a <pre>
Parameters
None |
Examples
Basic usage
Field::setData(['first' => 'Jim', 'last' => 'Jones']);
Field::debug();
// <pre>
// Array
// (
// [first] => Jim
// [last] => Jones
// )
// </pre>
displayIfEquals()
Combines the functionality of html()
and equals()
. Essentially removes the need to write an if statement.
Parameters
$field | required | Field name |
$val | required | Value to test against |
$pass | optional, default is "%s" | String to be displayed upon success. %s can be used similar to Field::html() to wrap html around $val |
$fail | optional, default is "" | String to be displayed when the field does not equal $val . |
Examples
// ['first' => 'John', 'last' => 'Smith']
Field::displayIfEquals('first', 'John');
// prints "John"
Field::displayIfEquals('first', 'John', 'First name is John');
// prints "First name is John"
Field::displayIfEquals('first', 'John', 'First name is %s');
// prints "First name is John"
Field::displayIfEquals('first', 'Jill', 'First name is Jill', 'Not a valid name');
// prints "Not a valid name"
allExist()
Check existance of all fields.
Parameters
$fields | required | Field names |
Examples
// ['first' => 'John', 'last' => 'Smith']
Field::allExist('first', 'last');
// true
Field::allExist('first', 'last', 'occupation');
// false
anyExist()
Check existance of any or all fields.
Parameters
$fields | required | Field names |
Examples
// ['first' => 'John', 'last' => 'Smith']
Field::anyExist('first', 'last');
// true
Field::anyExist('first', 'last', 'occupation');
// true
isArray()
Check whether the field is an array.
Parameters
$field | required | Field name |
Examples
// ['first' => 'John', 'dogs' => ['Ralph', 'Frank']]
Field::isArray('first');
// false
Field::isArray('dogs');
// true
count()
Count children of field. Returns null on non-array.
Parameters
$field | required | Field name |
Examples
// ['first' => 'John', 'dogs' => ['Ralph', 'Frank']]
Field::count('first');
// null
Field::count('dogs');
// 2
iterable()
Loop through current data set. Each iteration returns an instance of Loop
and sets up a new Field
class. This is intended to assist with repeating sets of nested fields.
Parameters
$field | required | Field name |
Example
// ['dataset' => [
['name' => 'Bill'],
['name' => 'John'],
]
foreach (Field::iterable('dataset') as $loop) {
echo $loop->index . ' - ' Field::get('name'); . '<br>'
}
// prints
// "0 - Bill"
// "1 - John"
relationship()
Loop through a relationship field. Each iteration sets up post data, returns an instance of Loop
and sets up a new Field
class.
Parameters
$field | required | Field name |
Example
// ['dataset' => [
['post' => ['post_title' => 'Post 1', ...]],
['post' => ['post_title' => 'Post 2', ...]],
]
foreach (Field::relationship('dataset') as $loop) {
echo $loop->index . ' - ' get_the_title(); . '<br>'
}
// prints
// "0 - Post 1"
// "1 - Post 2"