• Resolved labyrinthman

    (@labyrinthman)


    I have created google map meta-box for posts and I’m able to insert new values of the desired location. However, when I update the post I have to update the map as well or I will have an empty input.

    // /**
    //  * Save the metabox data
    //  */
     function wpt_save_events_meta( $post_id, $post ) {
    // 	// Return if the user doesn't have edit permissions. 
     	if ( ! current_user_can( 'edit_post', $post_id ) ) {
     		return $post_id;
     	}
    // 	// Verify this came from the our screen and with proper authorization,
    // 	// because save_post can be triggered at other times.
     	if ( ! isset( $_POST['lat'] ) || ! isset( $_POST['lng'] ) ||
       ! wp_verify_nonce( $_POST['map_fields'], basename(__FILE__) ) ) {
     		return $post_id;
     	}
    
    // 	// Now that we're authenticated, time to save the data.
    // 	// This sanitizes the data from the field and saves it into an array $events_meta.
     	$events_meta['lat'] = esc_textarea( $_POST['lat'] );
     	$events_meta['lng'] = esc_textarea( $_POST['lng'] );
    
     	$lat = esc_textarea( $_POST['lat'] ); 
     	$lng = esc_textarea( $_POST['lng'] );
    
    // 	// Cycle through the $events_meta array.
    // 	// Note, in this example we just have one item, but this is helpful if you have multiple.
     	foreach ( $events_meta as $key => $value ) :
     		// Don't store custom data twice
     		if ( 'revision' === $post->post_type ) {
     			return;
     		}
     		if ( get_post_meta( $post_id, $key, false ) ) {
     			// If the custom field already has a value, update it.
     			 update_post_meta( $post_id, $key, $value );  
     		} else {
     			// If the custom field doesn't have a value, add it.
     			add_post_meta( $post_id, $key, $value);
     		}
     		if ( ! $value ) {
     			// Delete the meta key if there's no value
     			delete_post_meta( $post_id, $key );
     		}
     	endforeach;
     }
     add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    When you say “update the map”, do you mean the map object in JavaScript? The location meta data for the map? Something else?

    Doesn’t your meta box get the currently saved values and populate the form fields with them, when they exist? Then your save meta code could check if the values in $_POST match the saved values or not before updating. If this is done, where would the empty input be coming from? How are you passing meta values to the map object to create the location marker?

    Thread Starter labyrinthman

    (@labyrinthman)

    Hello @bcworkz

    When you say “update the map”, do you mean the map object in JavaScript?

    Yes. The longitude and latitude are not updated from the database to the map object in JavaScript but instead, I have a blank input. If I want the location to be shown I need to specify the longitude and latitude every time I update the post. For instance, if I want to update the title of the post, I also have to update the longitude and latitude, if I want to update the text on my post then I also have to update the longitude and latitude etc…

    Doesn’t your meta box get the currently saved values and populate the form fields with them, when they exist?

    Yes, they get saved in the database but when I update a post I get blank input.

    Then your save meta code could check if the values in $_POST match the saved values or not before updating.

    That’s what I’m trying to do. I thought that this code will do that
    update_post_meta( $post_id, $key, $value );

    If this is done, where would the empty input be coming from?

    I presume from meta box where I put google map. If the marker on the map is not dragged to the desired position then the longitude and the latitude are empty.

    How are you passing meta values to the map object to create the location marker?

    The meta values go to the database then I use wp_localize_script function to create the location marker.

    Moderator bcworkz

    (@bcworkz)

    update_post_meta() will only skip the update where old and new values match under specific circumstances which are rather unlikely. It doesn’t do any harm to update with the same values, except needlessly running the update code.

    I misconstrued how you are using the map, so maybe I didn’t ask the best questions. But your answers gave me a better picture, so it’s all good.

    How is the marker location data in JS passed back to the server for saving? I’m guessing hidden meta box fields? This probably happens from a marker drop event. If one doesn’t drop the marker because its current position is correct, the fields have no value. The solution would be to pre-populate the meta box field values from the stored values during box initialization. If one moves the marker, the new values will overwrite the old. If no move occurs, the old values are in place in the event unrelated edits occur.

    BTW, your code doesn’t need to decide if it should update or add meta data. Calling update will also add if no data currently exists. The only reason add even exists is so update can call it if needed. I can’t think of any reason for us to use it. Update works for us in all situations I can imagine. It does no harm to decide whether to add or update, but it’s unnecessary.

    Thread Starter labyrinthman

    (@labyrinthman)

    @bcworkz

    Is there a function that can help me get a metabox value from a certain post? Or do I have to do it manually with custom code?

    Like you said to pre-populate the meta box field values from the stored values during box initialization.

    Moderator bcworkz

    (@bcworkz)

    I think you are looking for get_post_meta(). You will need to pass the post ID, which can be had from the post object which is passed to the metabox callback which does the output for the box.

    In my experience, the third parameter passed to get_post_meta() has invariably been true even though the default is false.

    Thread Starter labyrinthman

    (@labyrinthman)

    Thank you for your help @bcworkz.

    I have solved the problem.

    In the function that shows the map, I have added

    
    global $post;
    if ( current_user_can( 'edit_post', $post->ID ) ) {
     	$lat = get_post_meta( $post->ID, 'lat', true );
     	$lng = get_post_meta( $post->ID, 'lng', true );
    
    Moderator bcworkz

    (@bcworkz)

    Nice! You’re welcome!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Meta-box does not preserve value on update’ is closed to new replies.