Support

Account

Home Forums Backend Issues (wp-admin) ACF doesn't save text value (from previously handcoded meta box)

Solving

ACF doesn't save text value (from previously handcoded meta box)

  • ACF doesn’t save value and setting max_input_vars doesn’t help.

    The thing is that before I knew about ACF I had small custom code for adding meta box to my CTP. Now I have noticed that this is unnecessary so I would like to delete it and just add meta box with ACF Field Group. This is exactly what I did, and I have named my meta field “latin_name_branje” like I had it in my code.
    For previously added CTP posts this field is correctly populated but I can’t save value on a new posts, nor edit or delete previously set values on older posts.

    This was my piece of code for adding meta box

    class branje_biljaka {
    
    	public function __construct() 	{
    		...
    		$this->add_meta_box();
    	}
    
    	public function add_meta_box()	{
    
    		add_action('add_meta_boxes', function(){
    			add_meta_box('div', 'Latinsko ime', 'latin_name_branje', 'branje_biljaka', 'normal', 'high');
    		});
    
    		function latin_name_branje($post) 		{
    			$latin_name_branje = get_post_meta($post->ID, 'latin_name_branje', true);
    			?>
    			<p>
    				<input type="text" name="latin_name_branje" id="latin_name_branje" value="<?php echo esc_attr($latin_name_branje); ?>" />
    			</p>
    			<?php
    		}
    
    		add_action('save_post', function($id){
    			if(isset($_POST['latin_name_branje'])){
    				update_post_meta($id, 'latin_name_branje', strip_tags($_POST['latin_name_branje']));
    			}				
    
    		});
    
    	}
    
    }
    
    

    If I set this inside functions.php

    add_action('init', 'see_whats_posted');
    function see_whats_posted() {
      if ((!defined('DOING_AJAX') || !DOING_AJAX) && isset($_POST['acf'])) {
        echo '<pre>'; print_r($_POST['acf']); die;
      }
    }

    This is what I get, so I guess the problem is with this underscores while they should be hyphens.

    Array
    (
        [poveznica] => 9995
        [latin-name-branje] => asdf
    )

    How can I fix this, only by doing query on database or I can set something inside ACF? I thought I would just set the Field name for saving to correctly work, I though that Field label is just the label to show to users?

    🙂

  • 1) what version of acf are you using?
    2) Have you removed all of your own custom code related to this field?
    3) double check that the name you’ve used for the field in acf uses underscores and not dashes.

  • 1. I am using latest ACF PRO version.
    2. Yes

    If I change the Field name to “latin-name-branje” (Field label “Latin name branje”) then saving works but previously inserted fields which were under “latin_name_branje” ofcourse won’t show.

    Ah nvm, I am fiddling with this too long, for few hours. I have now placed again my code for manual meta box and this works so I will just stick to it…

    Thank you neverthless

  • My guess here is that the field is somehow is appearing twice on the edit page. If there is another occurrence of the same field on the admin page then the value of the duplicate field will overwrite the acf field. This could even be the standard WP custom meta field box.

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

The topic ‘ACF doesn't save text value (from previously handcoded meta box)’ is closed to new replies.