1

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

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.

1 Answer 1

2

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>';
2
  • This code wasn't tested so it can be a little bit buggy, I guess. But the idea behind it should be clear. Commented Jun 19, 2013 at 19:53
  • I was hoping to do it with WP's functions. But I guess cookies will do as fine. Thanks a lot for the reply!
    – bysanchy
    Commented Jun 20, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.