Support

Account

Home Forums ACF PRO ACF Form Change input name

Solved

ACF Form Change input name

  • Im using ACF Pro to generate a front end form, I need to have custom input name values for each field as I’m also posting the form data out to a third party also.

    Using acf/load_field I can target the input field, I can make this work with every other field value apart from ‘name’, any ideas?

    function acf_load_color_field_choices( $field ) {
    	$field['name'] = 'answer1';
        return $field;    
    }
    add_filter('acf/load_field/name=name', 'acf_load_color_field_choices');
  • Did you enter “name” into the field name when creating the field? If so then it should load, but I’m a bit confused at what you’re trying to do. If you change the field name then acf will not be able to load the values in the WP admin.

    You would be better off just naming the fields what they need to be named in order to send it to the 3rd party. Either that or use an acf/save_post filter to get the values after they are submitted, convert the field names and send the data to the 3rd party.

  • I’m submitting the form values to a third party which have preset variables for the input name field so I’m looking to get the acf fields to match this, at the moment my input fields look like:

    <input id="acf-field_5644ca0ecae0b" class="" type="text" placeholder="" value="" name="acf[field_5644ca0ecae0b]">

    I need input name to be something like:

    <input id="acf-field_5644ca0ecae0b" class="" type="text" placeholder="" value="" name="answer1">

    i can achieve this with jquery but doing this stops the data being being sent to the new post which is also made by the form, I either need acf to use custom field keys or look into acf/save_post filter

  • Yes, changing the names there will cause the data to fail being saved in the new post.

    You will need to use either an acf/save_post (http://www.advancedcustomfields.com/resources/acfsave_post/) filter of an acf/pre_save_post (http://www.advancedcustomfields.com/resources/acfsave_post/) filter.

    In the filter you can then get the values from the submitted fields and put them into an array with the names that you need to send to the 3rd party before sending the data.

  • thanks John

    I’ve got the form data retuning before the form data is saved, now I just need to figure out how to send the out as a url string

  • You can use several method, if your host allows fopen with a url string http://php.net/manual/en/features.remote-files.php

    if not then you will need to use CURL.

    Unless the place you’re sending it to requires something special.

    This really depends on where it’s going and there is a lot of information out there most of the possibilities.

  • I’ve found wp remote get and wp remote post which run GET or POST requests, I’ve wrapped a GET URL string in acf/save_post as you’ve suggested like so

    function my_acf_save_post( $post_id ) {
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            return;
        }
        // bail early if editing in admin
    	if( is_admin() ) {
    		return;
    	}
        // specific field value
        $field = $_POST['acf']['field_000000000'];
        $urlgetstring = "http://site.com/index.php?answer1=field";
    	
    	//send GET request
    	wp_remote_get( $urlgetstring );
    }
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    

    Thanks for the point in the right direction!

  • I have a similar problem, I’m loading fields using acf_form and manually specifying which fields I want by their name but when they load in the page, I still see their original id and name:

    <input id=”acf-field_5644ca0ecae0b” class=”” type=”text” placeholder=”” value=”” name=”acf[field_5644ca0ecae0b]”>

    How could I have them loaded with the name I assigned ?
    Thanks!

  • In order for the form to work and for ACF to save the values the field names need to be what ACF makes them, as I explained above. ACF uses the field keys in forms and neither the field key nor the field name can be altered if you want ACF to be able to save them.

  • I’ve found a workaround/cheat for this.

    You can create the form as usual, export it as json, modify the field keys in the json, save and import them 🙂

  • I will make the same comment here I made in the other thread for anyone that finds this.

    In 5 the field keys must begin with “field_”, while you can change the field keys the way you outline doing so will break ACF5.

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

The topic ‘ACF Form Change input name’ is closed to new replies.