Support

Account

Home Forums Add-ons Repeater Field Issue With Repeater Field Doc Example

Solving

Issue With Repeater Field Doc Example

  • I’ve come across an issue with adding new rows to a repeater field using the documentation example here:

    http://www.advancedcustomfields.com/resources/functions/update_field/

    /*
    *  add a repeater row on a taxonomy!!!
    */
     
    $field_key = "repeater_field";
    $post_id = "event_123";
    $value = get_field($field_key, $post_id);
    $value[] = array("sub_field_1" => "Foo", "sub_field_2" => "Bar");
    update_field( $field_key, $value, $post_id );

    The problem is that get_field() seems to replace some of the string characters with html entities, namely the & is replaced with &amp. This is fine usually, however when saving the value afterwards with update_field(), the update_field() function then seems to escape them out again. So if you have a value that has an & character and you add a few rows to your field later, you end up with &&& and it will continue adding & every time you add a new row.

    I hope this makes sense, its a bit hard to explain. Thanks!

  • At a quick glance it seems as though $double_encode should be set to false in the a couple of spots in the acf core when using htmlspecialchars:

    http://php.net/manual/en/function.htmlspecialchars-decode.php

    For now i just wrote a simple decode function that I run after get_field and before update_field:

    	public function htmlspecialchars_decode_array($array){
    		$newArray = array();
    		foreach($array as $arrayKey => $arrayVal){
    			$newArray[$arrayKey] = htmlspecialchars_decode($arrayVal);
    		}
    		return $newArray;
    	}
  • possibly a cleaner way around this:

    public function htmlspecialchars_decode_array($array){
    	$arrayJsonEncoded = json_encode($array,JSON_HEX_QUOT | JSON_HEX_APOS);
    	$arrayJsonEncodedSpecCharsDecoded =  htmlspecialchars_decode($arrayJsonEncoded);
    	$arrayJsonDecoded = json_decode($arrayJsonEncodedSpecCharsDecoded,true);
    	return $arrayJsonDecoded;
    }
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Issue With Repeater Field Doc Example’ is closed to new replies.