Created
May 3, 2011 11:35
-
-
Save bueltge/953193 to your computer and use it in GitHub Desktop.
WordPress Example Plugin: Comment meta data test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ) { | |
add_comment_meta( $comment_id, 'city', $_POST['city'] ); | |
} | |
add_filter( 'get_comment_author_link', 'attach_city_to_author' ); | |
function attach_city_to_author( $author ) { | |
$city = get_comment_meta( get_comment_ID(), 'city', true ); | |
if ( $city ) | |
$author .= " ($city)"; | |
return $author; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment