Skip to main content
All CollectionsCreate by MediavineAdvanced
Using Custom Fields with Create by Mediavine
Using Custom Fields with Create by Mediavine

In version 1.1+, you can now register custom fields to cards.

Sam Ellis avatar
Written by Sam Ellis
Updated over 4 years ago

Adding Custom Fields to a Create Card

Registering Custom Fields

In order to register custom fields to cards, use the mv_create_fields filter like so. (This code would go in your theme's functions.php file).

add_filter('mv_create_fields', function($arr) {
$arr[] = array(
'slug' => 'wwpoints',
'card' => 'recipe', // can also be an array
'type' => 'text', // can be 'text', 'textarea', 'select', or 'boolean'
'placeholder' => 'Point total',
'label' => 'Weight Watcher Points',
'instructions' => __('Enter the number of Weight Watcher points'),
);
return $arr;
});

$arr is an array of arrays, each representing a single custom field. Each field accepts the following options:

  • slug - (required) The unique key by which this value can be accessed

  • card - (optional) The card type or types to which the field belongs. If left empty, all cards will show this field.

  • type - (required) The type of UI to be displayed to users. Can be text, textarea, select, or boolean

  • label - (required) Label to be displayed in admin UI

  • instructions - (optional) Instructions to be displayed in admin UI

  • defaultValue - (optional) Default value

  • placeholder - (optional) Placeholder text to be displayed in admin UI (this will be overwritten by "defaultValue", if provided.

  • options - (required if type is "select") An associative array where key is value and value is label. This **must** be an associative array.

Down the road, we plan on add other options for the type param, including the ability to register custom UI components!


For convenience, you can use the function mv_create_register_custom_field which accepts an array for a single field as the argument.

$field = array(
'slug' => 'wwpoints',
'card' => 'recipe',
'type' => 'text', // can be 'text', 'textarea', 'select', or 'boolean'
'placeholder' => 'Point total',
'label' => 'Weight Watcher Points',
'instructions' => __('Enter the number of Weight Watcher points'),
);
mv_create_register_custom_field( $field );

Accessing Field Data

By ID

If you know the ID of a card to which you've added custom field data, you can use the `mv_create_get_field` function to access the value.

/**
* @param {int} $id ID of card
* @param {string} $slug Slug of field
*/
mv_create_get_field( $id, $slug );

For instance, to access the value of the example listed above for a recipe with the ID 42, you could call:

$ww_points = mv_create_get_field( 42, 'wwpoints' );

In Custom Template

If you've added custom templates to your theme, all custom field data is attached to the $args variable scoped to template files, at $args['creation']['custom_fields'];.

So, from the Weight Watcher Points example above, if you wanted to display the custom field content to your custom Recipe card template, you could add something like this to a template file:

<div>
<?php _e('Weight Watcher Points'); ?> <?php echo $args['creation']['custom_fields']['wwpoints']; ?>
</div>

Did this answer your question?