Front-end Editor
Checking Values of Custom Fields Before Save (5 posts)

  1. C. Spencer Beggs
    Posted 5 years ago #

    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.

  2. scribu
    Plugin Author

    Posted 5 years ago #

    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.

  3. C. Spencer Beggs
    Posted 5 years ago #

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

  4. C. Spencer Beggs
    Posted 5 years ago #

    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.

  5. scribu
    Plugin Author

    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Front-end Editor
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic