WordPress.org

Support

Support » Plugins and Hacks » Checking Values of Custom Fields Before Save

Checking Values of Custom Fields Before Save

  • Again, scribu. A great plugin. I have run across another quirk that I hope you or someone else here could help me out with:

    I have a two custom fields: ‘Rating’ and ‘Previous Rating.’ When someone edits the value of rating on the frontend, I want to check the current value of ‘Rating’ and if the value is different than the one being submitted, it should update the value of ‘Previous Rating’ with the current rating and then save the submitted value to rating. I saw a demo of hooking into wp_insert_post_data and I see that an AJAX call is being made to ajax_admin.php with my requested data. I am just not sure how it is available once I am inside wp_insert_post_data. Normally, I would look at $_POST.

    Here is the code I tried: http://pastebin.com/u8H9RQp3

    Any help or a point in the right direction would be appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author scribu
    Member

    @scribu

    wp_insert_post_data() wouldn’t be called, since FEE uses update_post_meta().

    There are several hooks in the underlying function – update_metadata() – which you could use. Careful not to enter into an endless loop.

    Thanks for the reply. I will check out the hook and be sure to report back what I find here.

    So, I have tried a lot of things to get this working. Hooking onto update_post_metadata() proved troublesome because I was, indeed, causing an infinite loop. I tried hooking on to the front-end-editor action itself, but I am getting strange results:

    function update_rating() {
    	global $wpdb, $post;
    	$data = $_POST['data'];
    	if ($data['key'] == 'Rating' && $_POST['callback'] == 'get') {
    		$currentrating = get_post_meta($data['post_id'], 'Rating');
    		update_post_meta($data['post_id'], 'Previous Rating', $currentrating);
    	}
    }
    add_action('wp_ajax_front-end-editor', 'update_rating', 9);

    For some reason that just deletes the ‘Previous Rating’ field all together. I tried to output the info to a new key with:
    add_post_meta($data['post_id'], 'Test', $currentrating);
    but that just ends up being empty. Any idea why this wouldn’t work? It seems like WordPress double saves things.

    Plugin Author scribu
    Member

    @scribu

    You probably only want a single value, so that would be:

    get_post_meta($data['post_id'], 'Rating', true);

    Can’t see any other errors.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Checking Values of Custom Fields Before Save’ is closed to new replies.