Support

Account

Home Forums Backend Issues (wp-admin) Create Field on the fly

Solving

Create Field on the fly

  • Hi!
    I want to achieve this:

    All textfields have an option called something like “translatble field”, if this is true i want to create a new textfield and append it to group field under the first field:
    For example if the textfield “main-text” has the “translatable” option to “true” a second field need to be generated named “main-text_en”.

    How can I create this second field on the fly?

    at the moment i have this:

    //creeate a radio input
    add_action(‘acf/render_field_settings/type=text’, ‘add_radio_to_field’, 20);
    function add_radio_to_field($field) {
    acf_render_field_setting( $field, array(
    ‘label’ => __(‘Translatable ‘,’acf’),
    ‘type’ => ‘radio’,
    ‘choices’ => array(
    0 => __(“No”,’acf’),
    1 => __(“Yes”,’acf’)),
    ‘name’ => ‘translatable’,
    ));
    }

    // check if field has the value
    function action_function_name( $field ) {
    if ($field[“translatable”])
    {
    echo “field is translatable”;
    }
    }
    add_action( ‘acf/render_field’, ‘action_function_name’, 10, 1 );

  • There isn’t any way to do this, that is add a field based on the selection in another field. What you need to do is add another field that uses conditional logic based on the first field. For an example of how to set up conditional logic set up a couple of fields with this conditional logic setup and export them to PHP to see how that setting of the field needs to be coded.

  • Thank you!

    Okay, maybe it is a better idea to use the duplicate field functionality of acf?
    Is it possible to add a new function that will do the same what the duplicate field function does but append another string (like “en”) to the new fields name? For example “duplicated-field_en”.

  • What you’re looking to do cannot be does easily. If I understand correctly, what you want to do is to somehow automatically add a field when editing an ACF field groups.

    Instead of building a new setting for an existing field to do what you want you might want to look into building a new type of field https://www.advancedcustomfields.com/resources/creating-a-new-field-type/. Doing this would let you add all your own settings and build the JS needed to make it work.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Create Field on the fly’ is closed to new replies.