• Hi there,

    Currently, I am working on a system that makes it more user friendly for customers to associate meta data with their posts using WordPress’s custom fields feature. It’s a bit like this, except I’m building a small object-oriented system for easier customization and extension.

    Basically, the system now works. I can enter my custom metadata, it gets saved and displayed properly. However, a very weird problem appears when doing a file upload.

    I want to give the customer the opportunity to add a thumbnail to a post using a file upload form. The code that gets invoked when a file is uploaded looks somewhat like this:

    if(!empty($_FILES[$this->ikey.'_attach']['name']))
    		{
    			$upload = wp_handle_upload($_FILES[$this->ikey.'_attach'], array('action' => 'editpost'));
    			$this->data = $upload['url'];
    			return true;
    		}

    After which what’s now stored in $this->data gets saved using update_post_meta. The file gets uploaded allright, and I verified the contents of $upload which are perfectly in order. However, it appears a blank meta value is written to the database. The really strange thing here is that the code works perfectly for all possible values of $this->data, except for $upload[‘url’]; For example, if I change it to this:

    if(!empty($_FILES[$this->ikey.'_attach']['name']))
    		{
    			$upload = wp_handle_upload($_FILES[$this->ikey.'_attach'], array('action' => 'editpost'));
    			$this->data = 'Some random crap';
    			return true;
    		}

    then the value ‘Some random crap’ gets written to the database. I find this behaviour very strange, so I took a look at my MySQL logs. I can actually see the meta value being updated to the right URL, after which its get updated again… to an empty string. This only happens when I assign $upload[‘url’] to $this->data. In all other cases the system works perfectly fine, even when I manually enter an URL that’s the same as what $upload[‘url’] is going to be!

    So, this problem’s got me stumped for a couple of hours now and I didn’t come any closer to a solution. If anyone knows why WordPress is overwriting my URL with an empty string, I’d be very thankful!

    – Jurre

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Jurre

    (@jurre)

    As usual, I solved my own problem. Turns out WordPress’s save_post hook gets called twice by default (why? it’s not in the codex). The second time it ran my submit function the upload would fail and update the post meta with the empty string.

    how you solved the problem?

    Thread Starter Jurre

    (@jurre)

    I simply added a member variable $this->submitted that gets set to true when the data has been saved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘update_post_meta: data gets overwritten when uploading a file’ is closed to new replies.