This topic contains 4 replies, has 2 voices, and was last updated by  thor 6 days, 18 hours ago.

  • Author
    Posts
  • #9388

    thor
    Participant

    I have a regular text field that holds a custom title (‘custom_title’) for the post. The title has a few “reserved” characters for soft hyphenation and line breaks (easy for the “non-tech-savvy” user to understand).

    I want to save that title to a an extra hidden field with the linebreaks as html tags. I know how to do that, but how do I pull the custom title field to the hidden? Do I need to hook into ‘piklist_save_field’? I would love to have it as a part of a the sanitization filter that I am already using to strip out extra spaces on the ‘custom_title’.

  • #9391

    Steve
    Keymaster

    You can create a hidden field by setting the field type to hidden.

    https://docs.piklist.com/fields/types/hidden/

  • #9400

    thor
    Participant

    Thanks Steve, I know how to create the hidden field. But how can it use the non-hidden text field? So I have the hidden field with the HTML version of the custom_title?
    I can run a manual ‘update_post_meta’ in my sanitize callback where I store the HTML version in a meta field. But this is not ideal for serialized arrays (when I have used Group fields)

  • #9403

    Steve
    Keymaster

    Can you post some code so we can take a look?

  • #9406

    thor
    Participant

    Right know I have this meta-box field

    
    /*
    Title: Intro
    Post Type: projects
    */
    
    piklist('field', array(
    	'type' => 'text',
    	'field' => 'project_introtitle',
    	'label' => 'Project title',
    	'columns' => 4,
    	'sanitize'  => array(
    		array(
    			'type' => 'title_sanitization', // defined in functions.php (piklist-functions.php)
    			'options' => 'html' // used as custom field suffix
    		)
    	),
    	'attributes' => array(
    		'placeholder' => get_the_title()
    	)
    ));

    In functions.php I have this sanitization and a bit of a hack for saving the field in a html version:

    // Piklist sanitize titles (Allow forced breaks) and soft hyphens
    add_filter('piklist_sanitization_rules', 'sanitize_title_function', 11);
    function sanitize_title_function($sanitization_rules) {
    	$sanitization_rules['title_sanitization'] = array(
    		'callback' => 'sanitize_title_callback'
    	);
    	
    	return $sanitization_rules;
    }
    
    function sanitize_title_callback($value, $field, $options) {
    	$returntitle = trim(preg_replace('/\s+/', ' ', $value)); // removes any extra (unwanted) spaces
    	$postid = intval( $field["object_id"] );
    	$this_field = $field["field"];
    	
    	if (isset($options) && '' !== $options && !is_array($options)) {
    		$suffix = '_' .sanitize_html_class($options);
    		$htmltitle = str_replace('|', '<br />', $returntitle);
    		$htmltitle = str_replace('_', '&shy;', $htmltitle);
    		update_post_meta( $postid, $this_field . $suffix, $htmltitle ); // extra meta (using suffix)
    	}
    	return $returntitle;
    }

    So this is a bit hacky, and it will not work serialized arrays. So what would be the proper solution – saving to a hidden piklist field that works with groups and repeaters?

You must be logged in to reply to this topic.