Introduction to Custom Fields

Creating Custom Fields in Cuisine is very easy. It’s a way to add new data to a post or to the options table. And when you’re doing it in code, it means you can add it to version-control, which is the sole reason we created the field-engine in Cuisine.

The Field-class is available through Cuisine’s Wrapper System, so you can start using it by adding

use Cuisine\Wrappers\Field

to the top of your php-document.


Available attributes

All fields have a name, a label and a type. The most basic form of creating a new instance of the Field class needs all three of these attributes:

	$type = 'text';
	$name = 'your_name';
	$label = 'Your Label';

	$field = Field::$type( $name, $label );

This field will end up getting posted as a regular text-field under the name ‘your_name’. Other attributes will be stored in the $properties array of each field. You can retrieve properties with a simple function call:

	$field = Field::$type( $name, $label, [ 'placeholder' => 'My placeholder' ]);
	$placeholder = $field->getProperty( 'placeholder' ); //returns 'My placeholder'
	

This function call will return false, since we’ve never set the placeholder to begin with. But you can pass an array of properties to each field. We dive a little deeper into this on the Using Custom Fields page.


Available methods

All fields share a common class which lays out the defaults. It’s located in /Classes/Fields/01-DefaultField.php in Cuisine and has the following public methods:


Get & Set methods:

Alter a fields properties with these fields

getValue();

Get the value of this field. The value depends on a few factors: it will check of the value is saved to the current post (if there is a current post) using the fields name, otherwise it will default to the ‘defaultValue’ attribute. If the ‘defaultValue’ attribute isn’t set it will return false, like the get_field() function in Advanced Custom Fields.

 
	Field::text( 'my-field', 'My Field' )->getValue();
/*
 * This first tries to return the value for get_post_meta( $post->ID, 'my-field', true );
 * If that's not set it will return self::getDefault();
 * If that also fails, it will return false.
 */


getDefault();

Returns the ‘defaultValue’ property if it’s set. Otherwise it will return false.

 
	Field::text( 'my-field', 'My Field' )->getValue();
/*
 * Returns self::getProperty( 'defaultValue' ) if it's set.
 * If that fails, it will return false.
 */


setName( $value );

Sets the name variable for this field after the field has been created.

 
	$field = Field::text( 'my-field', 'My Field' );
$field->setName( 'my-other-field' );
echo $field->name //returns 'my-other-field'.


Build methods:

These methods either return or straight-up echo the HTML for a field:

build();

The build function returns the html of just the input-element(s). It doesn’t get you a wrapper or a label.

 
	Field::text( 'my-field', 'My Field' )->build();
//returns:
'<input type="text" name="my-field" class="field input-field field-my-field type-text">';


render();

The render function echoes all the html for this field, including the wrapper, the label and custom classes.

	Field::text( 'my-field', 'My Field' )->render();
//echoes:
'<div class="field-wrapper text">
 
 		<label for="211b2e0ca31b24032d930f07c9a98aaa">My Field</label>
 		<input type="text" id="211b2e0ca31b24032d930f07c9a98aaa" class="field input-field field-my-field type-text" name="my-name">
 
</div>';
 


getLabel();

Returns the html for the label of this field

 
	Field::text( 'my-field', 'My Field' )->getLabel();
//returns:
'<label for="211b2e0ca31b24032d930f07c9a98aaa">My Field</label>';