• Resolved kuckovic

    (@kuckovic)


    Hi guys!

    I’m stuck…

    What I’m trying to accomplish:
    – I have created some metaboxes, and they work just fine. The problem is, I need a metabox, where I can add as many fields as I need – imagine like a shopping list – “add new item”….

    What have I already tried:
    I’ve been following this guide – but without luck. I am able to CREATE the new “item” – but it doesent save it.

    This is what my “save” looks like:

    function tr_save_meta($post_id, $post) {
    
    	// Return user if he/she does not have the permission to do this!
    	if(!current_user_can('edit_post', $post_id)) {
    		return $post_id;
    	}
    	
    	if(!isset($_POST['tr_items']) || !wp_verify_nonce($_POST['tr_items_nonce'], basename(__FILE__))) {
    		return $post_id;
    	}
    
    	// Okay, we′re authorized - let′s save the data!
    	$tr_meta['tr_items'] = esc_textarea($_POST['tr_items']);
    
    	// Let′s cycle through the "$tr_meta" array
    	foreach($tr_meta as $key => $value) :
    
    		// Let′s not save the data twice
    		if('revision' === $post->post_type) {
    			return;
    		}
    
    		if(get_post_meta($post_id, $key, false)) {
    
    			// If the field already has a value, we will update it...
    			update_post_meta($post_id, $key, $value);
    		
    		} else {
    
    			// If the field is empty, we will add the value
    			add_post_meta($post_id, $key, $value);
    
    		}
    
    		// If there is no value, we will delete the meta key
    		if(!$value) {
    			delete_post_meta($post_id, $key);
    		}
    
    	endforeach;
    
    }
    
    add_action('save_post', 'tr_save_meta');
    

    I’m confused…
    What am I doing wrong?
    Am I turning blind to my own code??

    Thanks in advance!
    Aris Kuckovic

Viewing 3 replies - 1 through 3 (of 3 total)
  • Anonymous User 5746546

    (@anonymized-5746546)

    Quick question, something I learned recently from one of my help posts… does your error log say anything?

    I was also thinking about which object is the object you want to “save” to. Here you are passing a string ID and (I think) a $post object:

    
    function tr_save_meta($post_id, $post)
    

    But are you saving to the $post object or are you saving it to some other object when you call update_post_meta()? That is, instead of passing $post_id, is there a difference if you passed $post->ID?

    Those are just some of my thoughts. Hope it helps you narrow it down. Sorry I couldn’t be of better help!

    • This reply was modified 7 years ago by Anonymous User 5746546.
    Moderator bcworkz

    (@bcworkz)

    When you have a meta box with variable content, you need to save the entire current configuration somewhere. Then when you load the meta box, go to this location to get the configuration. If the configuration does not exist, you can output the default. This is just an extension of normal meta box save value logic, except you save the entire configuration instead of a single value.

    Where and how to save depends on what your needs are. Is the configuration the same for all users, and unique to the post? Unique to the user for all posts? Unique for both? What table and how it is keyed will vary by your need.

    BTW, you can save an entire array ($tr_meta for example) under a single meta value if you like. WP will automatically serialize and unserialize the array. However, querying by a meta value is difficult if it is in a serialized array. Any values that may need to be part of query criteria should be saved individually.

    Thread Starter kuckovic

    (@kuckovic)

    Quick question, something I learned recently from one of my help posts… does your error log say anything?

    – Hi LB,

    Sadly not.
    But I’ve managed to make it work by following bcworkz’s advice.
    So thanks a lot, bcworkz and LB – I just needed a good nights sleep ??

    I changed:
    $tr_meta['tr_items'] = esc_textarea($_POST['tr_items']);

    To:
    $tr_meta = $_POST['tr_items'];

    Now it’s working as it should.
    Thanks a lot, guys.

    Best regards
    Aris Kuckovic

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add new input field in meta box’ is closed to new replies.