Home Forums Backend Issues (wp-admin) Set default value for all empty fields on save


Set default value for all empty fields on save

  • Hello,

    I have several posts of type “Product”, and my client needs to leave fields blank if they aren’t relevant to the specific product. I need to add functionality that assigns a value of “blank” to any fields that are empty when the post is saved.

    The purpose for this is to hide any rows in a table that are empty. To create the tables on the frontend, I’m populating TablePress with ACF fields using the ACF shortcode. TablePress can remove table rows that have a word you want to check for, but it can’t identify whether rows are empty or not. So I need to dynamically populate all empty fields with a filter word (“blank” in this case) so TablePress can hide the irrelevant rows. While I could easily hide empty rows with javascript, I can’t use javascript because I also need to convert the page content to a PDF, and the HTML to PDF parser does not seem to support javascript.

    Now I’ve seen several posts here that describe how to assign a value to a specific field by name. But if possible/practical, I need to loop through all the fields in a post and carry out the same action for each one that is empty, and I haven’t been able to find an example. Could someone please review my code and recommend improvements or suggest a better method? I haven’t tested this yet, but will shortly. Thank you!

    function set_defaults_on_save_post( $post_id ) {
        // Get newly saved values.
        $values = get_fields( $post_id );
        // Loop through post fields, adding 'blank' to empty ones.
        foreach( $fields as $field ) {
    		if ( !$field['value'] =='' ) {
    			$field[ 'value'] = 'blank';
    add_action('acf/save_post', 'set_defaults_on_save_post');
  • Ok, well I’ve got something that works now. But as I am a newbie to ACF snippets, I would be very grateful if someone would check this over and let me know how I might improve it. Thanks!

    /* Fill empty ACF fields with "blank" so TablePress can hide them */
    function set_empty_value( $post_id ) {	
    	if ( 'product' != get_post_type($post_id) ) {
    	$fields = get_fields();
    	if( $fields ) {
    		foreach ( $fields as $name => $value ) {
    			if ( $value == '' ) {
    				$value = "blank";
    				update_field( $name, $value );
    add_action('acf/save_post', 'set_empty_value', 10, 1 );
  • You could probably be more specific. For example limiting fields to only text fields by using the acf/update_value hook. I would be concerned with setting the wrong type of values for each type of field by looping though all the fields the way you are doing. Unless you’re sure that “blank” is an appropriate value for every field.

    You can also set the default value for almost every field in ACF by editing the fields and this would eliminate the need do this….. unless they already have some default value that’s being deleted.

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

You must be logged in to reply to this topic.