Skip to content

Third Party Fields

Jörn Lund edited this page Aug 31, 2018 · 4 revisions

How to Support your own Fields

The plugin uses the ACF JS function to add a copy of the duplicated row or layout. After adding the field values are copied from the source field to the destination.

Before the field value is copied, the source field triggers a jQuery Event acf_duplicate:<field_type>. To implement your own field duplication support, just add an event listener, and do whatever is necessary to copy the values.

Use wp_add_inline_script() to integrate it in the WordPress admin.

Generic Example

$(document).on('acf_duplicate:some_field_type', function(e){
    var $src = $(e.target),
        $dest = e.destination;

    $dest.val( $src.val() );


    e.preventDefault();
});

Real World Example

Table Field: https://de.wordpress.org/plugins/advanced-custom-fields-table-field/

$(document).on('acf_duplicate:table', function(e){
    var $src = $(e.target),
        $dest = e.destination;

    $dest.find('.acf-table-table').html( $src.find('.acf-table-table').html() );
    $dest.find('select').val( $src.find('select').val() ).trigger('change');

    e.preventDefault();
});

WordPress Integration

<?php
add_action( 'admin_enqueue_scripts', function() {

    $scpt = <<<'EOD'
(function($){
    $(document).on('acf_duplicate:table', function(e){
        var $src = $(e.target),
            $dest = e.destination;

        $dest.find('.acf-table-table').html( $src.find('.acf-table-table').html() );
        $dest.find('select').val( $src.find('select').val() ).trigger('change');

        e.preventDefault();
    });
})(jQuery);
EOD;

    wp_add_inline_script( 'acf-repeater-duplicate-admin', $scpt );

});
?>