Support

Account

Home Forums ACF PRO Date Picker: Using escape-characters results in odd behaviour

Solving

Date Picker: Using escape-characters results in odd behaviour

  • Dear Elliot,

    If you use backslash-character in the display format field for the date picker, ACF does some quite unpredictable behaviour.

    Use case: I was entering a little string into the “Display Format”. In my country a common date format similar to “Sunday *the* 31st *of* December” is quite used, but to write such a string, I wanted to backslash all the characters, so the display format goes like:

    “l \d\e\n j. F Y”

    I’ve experienced these behaviours with minor modifications to this string:

    * The field reverted to a text-field.
    * The field was removed from the current Field Group and a new Field group with only this field was added – still as a text-field. At the same time WordPress hung.

    Kind regards,
    Morten Skyt

  • Hi @mortenskyt

    Thanks for the bug report. It sounds like the Serialized array of field data is becoming corrupt with the backslash character…

    Any chance your site / theme contains a constant called DISALLOW_UNFILTERED_HTML?

  • I’m having this issue as well – any field that has a backslash in any of its values (prepend, append, default text, custom, etc), is reverted to a text field type and loses the settings of the other field type. I do not have DISALLOW_UNFILTERED_HTML defined on my site.

  • I was able to fix this by changing api/api_field.php on line 789 from:

    $data = maybe_serialize( $data );
    

    Once single slashes are replaced with double slashes (which then get escaped back to a single slash in the database post_content) it saves the proper serialized data and can be loaded correctly.

    $data = maybe_serialize( $data );
    $data = str_replace('\\', '\\\\', $data);
    
  • I was also able to correct this using a filter to avoid changing the ACF code directly, the preg_match() will only do the replace if there is a single \ in the content, not a double \\:

    add_filter( 'content_save_pre', function( $content ){
    	global $post;
    	if ( get_post_type() == 'acf-field-group' ){
    		if ( preg_match( '~(?<!\\\\)\\\\(?!\\\\)~', $content ) ){
    			$content = str_replace('\\', '\\\\', $content);
    		}
    	}
    	return $content;
    });
    

    I’m including this filter in my Validated Field plugin since backslashes are pretty common in regular expressions which is one of the validations types I am supporting.

  • I added a more complete report here, sample code to fix the upgrade and save. Not sure what the proper way to address this in ACF is, however it is pretty serious as it loses the configuration for any field with a backslash anywhere in its serialized data….

    http://support.advancedcustomfields.com/forums/topic/backslashes-in-field-attributes-breaks-acf5-import-save/

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

The topic ‘Date Picker: Using escape-characters results in odd behaviour’ is closed to new replies.