public
Last active

WordPress Example Plugin: Comment meta data test

  • Download Gist
commentmeta-multi.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php
/*
Plugin Name: Comment meta data test
Version: 1.0
Plugin URI: http://wpengineer.com
Description: Comment meta data test
Author: Latz
Author URI: http://wpengineer.com
*/
 
 
add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
 
function change_comment_form_defaults($default) {
 
$commenter = wp_get_current_commenter();
 
$default['fields']['email'] .= '<p class="comment-form-author">' .
'<label for="city">'. __('City') . '</label>
<span class="required">*</span>
<input id="city" name="city" size="30" type="text" /></p>';
return $default;
}
 
 
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data($commentdata) {
if ( ! isset( $_POST['city'] ) )
wp_die( __('Error: please fill the required field (city).') );
return $commentdata;
}
 
 
add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
$cities = explode(',', $_POST['city']);
foreach ($cities as $city)
echo update_comment_meta( $comment_id, 'city', $city, true);
}
 
 
add_filter( 'get_comment_author_link', 'attach_city_to_author' );
function attach_city_to_author( $author ) {
$cities = get_comment_meta( get_comment_ID(), 'city', false );
if ( $cities ) {
$author .= ' (';
foreach ($cities as $city)
$author .= $city . ' ';
$author .= ')';
}
return $author;
}
 
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.