Auto-fill Custom comment fields

Thank you for reading,

I added custom fields to the comment form, and saved the input in the DB, following this tutorial http://wp.smashingmagazine.com/2012/05/08/adding-custom-fields-in-wordpress-comment-form/comment-page-1/#comment-115187

Read More

Now i’d like the meta-fields in the comment form to auto-fill after the user has commented once.

Just like wordpress does with the Name, E-mail and Url; once you’ve commented the value stays in the fields. However, wordpress does this with $commenter[‘comment_author’] and this array doesn’t have the data I’m looking for.

Related posts

1 comment

  1. You can store these values in cookies and fill them when you are creating form inputs.

    So in save_comment_meta_data add something like this:

    $commenter_data = array(
      'phone' => $phone,
      ...
    );
    setcookie('commenter_data', serialize($commenter_data), time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
    

    And then when you’re creating form:

    $commenter_data = isset($_COOKIE['commenter_data']) ? unserialize($_COOKIE['commenter_data]) : array();
    echo '<p class="comment-form-title">'.
    '<label for="phone">' . __( 'Commenter Phone' ) . '</label>'.
    '<input id="phone" name="phone" type="text" size="30"  tabindex="5" value="'. (array_key_exists('phone', $commenter_data) ? $commenter_data['phone'] : '') .'" /></p>';
    

Comments are closed.