Nuxy
Search…
Registering field
We have quite enough fields to use, but if you need something extra functional and you familiar with Vue and with Components you can make your own fields type.
Here we will explain how to create own field called Repeater
First of all, you should specify the directory to add a custom field template:
my-plugin/my-own-fields/setup.php
In your config file specify field type.
1
'awesome_field' => array(
2
'type' => 'my_super_field',
3
'label' => esc_html__('Simple Repeater', 'my-domain'),
4
'value' => 'Awesome default value',
5
'submenu' => esc_html__('General fields', 'my-domain'),
6
),
Copied!
Now we have a new field type my_super_field
1
add_filter('wpcfto_field_my_super_field', function($path){
2
3
return dirname(__FILE__) . '/my_super_field.php';
4
5
});
Copied!
The framework will watch for the directory you specified above with template name my_super_field.php
In the template will be available certain variables : /path/to/my-folder/my_super_field.php
1
<?php
2
3
/**
4
* @var $field
5
* @var $field_id
6
* @var $field_value
7
* @var $field_label
8
* @var $field_name
9
* @var $section_name
10
*
11
*/
Copied!
And we need to enqueue our component script
1
wp_enqueue_script('my-super-component', plugin_dir_url(__FILE__) . '/js_components/my_super_field.js');
Copied!
Now we need to create Component with Repeater Logic, encapsulated from the main app.
1
<label v-html="<?php echo esc_attr($field_label); ?>"></label>
2
3
<my_super_component inline-template
4
v-on:get-super-mode="<?php echo esc_attr($field_value); ?> = $event"
5
v-bind:super_data="<?php echo esc_attr($field_value); ?>">
6
7
<div class="super-awesome-component">
8
9
<div v-for="(single_super, single_super_key) in data" class="row">
10
<div class="column">
11
<input type="text" v-model="data[single_super_key]"/>
12
</div>
13
<div class="column">
14
<i class="fa fa-times"
15
v-bind:style="removeStyles"
16
@click="removeSuperSingle(single_super_key)" class="column"></i>
17
</div>
18
</div>
19
20
<div class="button" @click="addSuperSingle">Add Super Field</div>
21
22
</div>
23
24
</my_super_component>
Copied!
Now we need to specify input field to store our setting
1
<!--Here We store actual value in hidden input-->
2
<!--Mostly it needed for metabox area, where WordPress saves field automatically after post update-->
3
<input type="hidden"
4
name="<?php echo esc_attr($field_name); ?>"
5
v-bind:placeholder="<?php echo esc_attr($field_label); ?>"
6
v-bind:id="'<?php echo esc_attr($field_id); ?>'"
7
v-model="<?php echo esc_attr($field_value); ?>"/>
Copied!
So final code in /path/to/my-folder/my_super_field.php looks like:
1
<?php
2
3
/**
4
* @var $field
5
* @var $field_id
6
* @var $field_value
7
* @var $field_label
8
* @var $field_name
9
* @var $section_name
10
*
11
*/
12
13
wp_enqueue_script('my-super-component', plugin_dir_url(__FILE__) . '/js_components/my_super_field.js');
14
15
?>
16
17
<label v-html="<?php echo esc_attr($field_label); ?>"></label>
18
19
<my_super_component inline-template
20
v-on:get-super-mode="<?php echo esc_attr($field_value); ?> = $event"
21
v-bind:super_data="<?php echo esc_attr($field_value); ?>">
22
23
<div class="super-awesome-component">
24
25
<div v-for="(single_super, single_super_key) in data" class="row">
26
<div class="column">
27
<input type="text" v-model="data[single_super_key]"/>
28
</div>
29
<div class="column">
30
<i class="fa fa-times"
31
v-bind:style="removeStyles"
32
@click="removeSuperSingle(single_super_key)" class="column"></i>
33
</div>
34
</div>
35
36
<div class="button" @click="addSuperSingle">Add Super Field</div>
37
38
</div>
39
40
</my_super_component>
41
42
43
<!--Here We store actual value in hidden input-->
44
<!--Mostly it needed for metabox area, where WordPress saves field automatically after post update-->
45
<input type="hidden"
46
name="<?php echo esc_attr($field_name); ?>"
47
v-bind:placeholder="<?php echo esc_attr($field_label); ?>"
48
v-bind:id="'<?php echo esc_attr($field_id); ?>'"
49
v-model="<?php echo esc_attr($field_value); ?>"/>
Copied!
And our JS component file: /path/to/my-folder/js_components/my_super_field.js
1
Vue.component('my_super_component', {
2
props: ['super_data'],
3
data: function () {
4
return {
5
data: [],
6
removeStyles: {
7
color: 'red',
8
padding: '15px 10px'
9
}
10
}
11
},
12
mounted: function () {
13
if (WpcftoIsJsonString(this.super_data)) {
14
this.data = JSON.parse(this.super_data);
15
} else {
16
this.data.push(this.super_data);
17
}
18
19
},
20
methods: {
21
removeSuperSingle: function (index) {
22
this.data.splice(index, 1);
23
},
24
addSuperSingle: function () {
25
this.data.push('');
26
}
27
},
28
watch: {
29
data: function (value) {
30
this.$emit('get-super-mode', JSON.stringify(value));
31
}
32
}
33
});
Copied!
Field looks like this now:
And file structure:
Last modified 10mo ago
Copy link