WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Meta Data being corrupted or altered (5 posts)

  1. pha3z
    Member
    Posted 1 year ago #

    I've got a single meta variable with a string value that should always be 0 or 1 being stored on pages. I can set it using a simple dropdown box in a metabox and it seems to work great. For some bizarre reason, it sometimes gets changed from 1 to 0 seemingly randomly. I have no clue why, and it seems to happen even on a page I wasn't editing or updating. I'm baffled and don't even know where to begin looking because I haven't figured out how to replicate the problem.

    Does anyone have any suggestions to search for a possible way to replicate this or figure out the cause?

  2. Shaun Scovil
    Member
    Posted 1 year ago #

    What is the meta key? If it is not unique and some other plugin or theme function is using it as well, that could explain why it gets overwritten seemingly at random...

  3. pha3z
    Member
    Posted 1 year ago #

    That is an extremely good point. But that's definitely not the problem. Its a very unique name.

    I actually think I've solved the problem, but I'm not positive since I never did manage to figure out how to replicate it. I ended up adding some conditional logic before the UPDATE occurs. The logic checks to see if the $_POST value is set before doing anything. Obviously, it would have been smart to have that there before, but it appears it wasn't. I think what was happening is that the save_meta_data function was getting called from certain actions on the site wherein the POST data wasn't present since it wasn't in the standard page update/publish method I'd expect. Since the value was not set, PHP was just tripping over it and storing a null or zero value.

    The thing that turned me onto this possible cause was that I did a Bulk Edit operation on all the pages on the site, and I discovered that Bulk Edit causes all the save_meta_data functions I created to trigger because they are tied to the post update action. Obviously when you do a Bulk Edit, all those POST vars you might expect aren't there. I saw a PHP NOTICE about an unset index for POST, and that's what tipped me off.

    - Jim

  4. Shaun Scovil
    Member
    Posted 1 year ago #

    Hmmm. I know it may seem obvious, but are you setting the default value of your meta box input using get_post_meta()? Or is the default input value just left to 0 (or NULL)?

    For example, in this function...

    <?php
    function my_metabox_settings( $post ) {
    
    	$settings = get_post_meta( $post->ID ); // get an array of all post meta
    	$value = esc_attr( $settings['my_meta_key'][0] ); // get the saved value for your meta key
    	$domain = 'my_text_domain'; // used for internationalization
    
    ?>
    
    	<table class="form-table">
    		<tr class="my_metabox_settings">
    			<th>
    				<label for="my_boolean_option"><?php _e( 'Do this thing?', $domain ); ?></label>
    			</th>
    			<td>
    				<input type="checkbox" name="my_boolean_option" value="1" <?php if( $value == '1' ) echo 'checked'; ?> /> <?php _e( 'Yes, do this.', $domain ); ?>
    			</td>
    		</tr>
    		<?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
    	</table>
    
    <?php
    } // end of function my_metabox_settings()
    ?>

    ...the line <?php if( $value == '1' ) echo 'checked'; ?> tells the checkbox to be checked only if the current value is set to 1.

    If you aren't doing that, it would explain why the setting was getting reset each time you saved a post without changing that meta value.

  5. pha3z
    Member
    Posted 1 year ago #

    Yes, my code has been setting the value. Good suggestion, though.

    The problem wasn't happening every time I saved. I was trying to explain in my original post that the problem has been seemingly random. It usually doesn't happen when I save the post. And I haven't been positive but I thought it was even happening when I was doing other operations on other posts. I couldn't ever replicate it.

    Fortunately, since I made the change to check to see if $_POST is set, the problem seems to be gone now. I still never figured out just exactly what action I was taking that was causing it to change, but it hasn't happened again (yet).

    I'm certain its not the meta box default value because that is properly setting and representing the current database value. But thanks for the suggestion on something to look into!

    ---------------------------------------------------------------
    I'm considering this issue as resolved. I should have marked it resolved before. I'll do it now.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.