Support

Account

Home Forums General Issues Save/post data to Acf field default values?

Solved

Save/post data to Acf field default values?

  • What I am trying to accomplish
    I would like to use an image instead of a map in the leaflet.js.

    So i did some googling and found advanced-custom-fields-leaflet-field, It was a great starting point, so I striped it down to the bare bones and used it in conjunction with Acf Image Field. The user will need to be able to browse the media library to find an image to put in the ACF leaflet.js code.

    The ACF leaflet comes with map markers functionality so you can place markers on images/maps. When I hit update button the markers save/post data overwrites the image attachment save/post data.

    I know why (they are targeting the save field value) the save/post data is getting overwritten but I just don’t know how to target a default “values” when save/post the data.

    // vars
            $this->name = 'leaflet_image_field';
            $this->label = __( 'Leaflet Image Field' );
            $this->category = __( 'Content','acf' ); // Basic, Content, Choice, etc
            $this->defaults = array(
                'save_format'   =>  'object',
                'preview_size'  =>  'thumbnail',
                'library'       =>  'all',
                'markers'       =>  '',// HOW DO I SAVE/POST DATA TO THIS?
                'lat'           => '55.606',
                'lng'           => '13.002',
                'zoom_level'    =>  5,
                'height'        =>  500,
            );
    
    h//ere is my inputs: in the "create_field" function  
    
    <input class="acf-image-value" type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" />
                    
                    <input id="field_<?php echo $uid; ?>" type="hidden" name="<?php echo $field['name'].'[markers]'; //could not get this to work ?>" value="<?php echo $field['markers'];

    Can anyone help me or point me to a tutorial?

    J

  • Hi @jwight1234

    If you look down at the create_field function, you shoudl see an input that has a name attribute of $field['name'].

    This is how ACF can POST the data to the correct name.

    If you are saving 1 value, then just get your JS to put the value into this input.
    If you are saving multiple values, then I will need to explain how to post an array in a later comment.

    Now, after you have saved a value, you can find the value (again in the create_field function) with $field['value']. ACF will already load the value for you and place it here!

    Easy

    Thanks
    E

  • Hi @elliot,
    Can you please explain saving multiple values with ACF?
    Please πŸ˜€
    J

  • Hi @jwight1234

    The easiest option is to use the repeater field to ‘repeat’ your custom field type. This involves no code updates to work.

    If you want just 1 field on the page, which can save multiple values, then you need to add in multiple input elements.

    Currently, you should have an input that has a name attribute of $field[‘name’].
    <input name="<?php echo $field['name'] ?>" value="something" />

    To save multiple values as an array, you can change this to:

    
    <input name="<?php echo $field['name'] ?>[]" value="val 1" />
    <input name="<?php echo $field['name'] ?>[]" value="val 2" />
    

    Adding in the extra [] to the name, allows you to save values as an array!

    Thanks
    E

  • That is great thank you πŸ™‚

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

The topic ‘Save/post data to Acf field default values?’ is closed to new replies.