Nuxy
  • Nuxy
  • Installation
  • Adding Page Options
  • Adding Post Options
  • Adding Taxonomy Options
  • Adding Frontend Options
  • Getting Fields
  • Adding to admin bar
  • Tabs
    • General tab parameters
    • Add submenu
  • Field parameters
  • Registering field
  • Fields dependency
  • Fields group
  • Field types
    • General field parameters
    • 1. Text
    • 2. Number
    • 3. Textarea
    • 4. Checkbox
    • 5. Select
    • 6. Radio
    • 7. Color
    • 8. Image
    • 9. Editor
    • 10. Autocomplete
    • 11. Datepicker
    • 12. Datepicker range
    • 13. Time
    • 14. Sortable
    • 15. Sortable multi input
    • 16. Gallery
    • 17. Repeater
    • 18. Range slider
    • 19. Image Select
    • 20. Button group
    • 21. Icon picker
    • 22. Ace Editor
    • 23. Spacings
    • 24. Link color
    • 25. Multi checkbox
    • 26. Color Gradient
    • 27. Textarea with hints
    • 28. Typography
    • 29. Multiselect
    • 30. Notification Message
    • 31. Button List
    • 32. Group Title with Preview && Icon
  • Download Nuxy
Powered by GitBook
On this page

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.

'awesome_field' => array(
    'type' => 'my_super_field',
    'label' => esc_html__('Simple Repeater', 'my-domain'),
    'value' => 'Awesome default value',
    'submenu' => esc_html__('General fields', 'my-domain'),
),

Now we have a new field type my_super_field

add_filter('wpcfto_field_my_super_field', function($path){

    return dirname(__FILE__) . '/my_super_field.php';

});

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

<?php

/**
 * @var $field
 * @var $field_id
 * @var $field_value
 * @var $field_label
 * @var $field_name
 * @var $section_name
 *
 */

And we need to enqueue our component script

wp_enqueue_script('my-super-component', plugin_dir_url(__FILE__) . '/js_components/my_super_field.js');

Now we need to create Component with Repeater Logic, encapsulated from the main app.

<label v-html="<?php echo esc_attr($field_label); ?>"></label>

<my_super_component inline-template
                    v-on:get-super-mode="<?php echo esc_attr($field_value); ?> = $event"
                    v-bind:super_data="<?php echo esc_attr($field_value); ?>">

    <div class="super-awesome-component">

        <div v-for="(single_super, single_super_key) in data" class="row">
            <div class="column">
                <input type="text" v-model="data[single_super_key]"/>
            </div>
            <div class="column">
                <i class="fa fa-times"
                   v-bind:style="removeStyles"
                   @click="removeSuperSingle(single_super_key)" class="column"></i>
            </div>
        </div>

        <div class="button" @click="addSuperSingle">Add Super Field</div>

    </div>

</my_super_component>

Now we need to specify input field to store our setting

<!--Here We store actual value in hidden input-->
<!--Mostly it needed for metabox area, where WordPress saves field automatically after post update-->
<input type="hidden"
       name="<?php echo esc_attr($field_name); ?>"
       v-bind:placeholder="<?php echo esc_attr($field_label); ?>"
       v-bind:id="'<?php echo esc_attr($field_id); ?>'"
       v-model="<?php echo esc_attr($field_value); ?>"/>

So final code in /path/to/my-folder/my_super_field.php looks like:

<?php

/**
 * @var $field
 * @var $field_id
 * @var $field_value
 * @var $field_label
 * @var $field_name
 * @var $section_name
 *
 */

wp_enqueue_script('my-super-component', plugin_dir_url(__FILE__) . '/js_components/my_super_field.js');

?>

<label v-html="<?php echo esc_attr($field_label); ?>"></label>

<my_super_component inline-template
                    v-on:get-super-mode="<?php echo esc_attr($field_value); ?> = $event"
                    v-bind:super_data="<?php echo esc_attr($field_value); ?>">

    <div class="super-awesome-component">

        <div v-for="(single_super, single_super_key) in data" class="row">
            <div class="column">
                <input type="text" v-model="data[single_super_key]"/>
            </div>
            <div class="column">
                <i class="fa fa-times"
                   v-bind:style="removeStyles"
                   @click="removeSuperSingle(single_super_key)" class="column"></i>
            </div>
        </div>

        <div class="button" @click="addSuperSingle">Add Super Field</div>

    </div>

</my_super_component>


<!--Here We store actual value in hidden input-->
<!--Mostly it needed for metabox area, where WordPress saves field automatically after post update-->
<input type="hidden"
       name="<?php echo esc_attr($field_name); ?>"
       v-bind:placeholder="<?php echo esc_attr($field_label); ?>"
       v-bind:id="'<?php echo esc_attr($field_id); ?>'"
       v-model="<?php echo esc_attr($field_value); ?>"/>

And our JS component file: /path/to/my-folder/js_components/my_super_field.js

Vue.component('my_super_component', {
    props: ['super_data'],
    data: function () {
        return {
            data: [],
            removeStyles: {
                color: 'red',
                padding: '15px 10px'
            }
        }
    },
    mounted: function () {
        if (WpcftoIsJsonString(this.super_data)) {
            this.data = JSON.parse(this.super_data);
        } else {
            this.data.push(this.super_data);
        }

    },
    methods: {
        removeSuperSingle: function (index) {
            this.data.splice(index, 1);
        },
        addSuperSingle: function () {
            this.data.push('');
        }
    },
    watch: {
        data: function (value) {
            this.$emit('get-super-mode', JSON.stringify(value));
        }
    }
});

Field looks like this now:

And file structure:

PreviousAdd submenuNextFields dependency

Last updated 4 years ago