i know this might sound a bit weird, but what I’m trying to do is rather simple in mind but may be technically difficult.
I’m running small contests on my wordpress site where I ask users to leave a comment. I’m selecting a comment after a certain period of time and this is the winner.
What I’d like to do now, is mark that comment as winner.
So, I’m thinking of a solution like this: When viewing the comments in /wp-admin I can set a comment as spam, approved, rejected, etc. I’d like to have another Link/Button that says: “winner”. If I click this link the comment has an additional class like <li class="comment odd alt thread-even depth-1 winner">
Is there a clever solution that comes to your mind? How can I do that?
Any other ideas?
Thank you in advance.
Matt
1 Answer
Assuming you’d like the facility to update this data from the quickedit box whilst viewing the list of comments, you’ll need a series of actions and filters. I’ve tried to make appropriate comments in the necessary places for you, though bear in mind i threw this all together for you with a small amount of testing(it does work though).
This should get give you a good starting point…
// Add new heading to comments tables
add_filter( 'manage_edit-comments_columns', 'winner_comment_column' );
function winner_comment_column( $columns ) {
$columns['winning_comment'] = __( 'Winning Comment' );
return $columns;
}
// Print comment meta for new meta column
add_action( 'manage_comments_custom_column', 'myplugin_comment_column', 10, 2 );
function myplugin_comment_column( $column, $comment_ID ) {
if ( 'winning_comment' != $column )
return;
echo esc_attr( get_comment_meta( $comment_ID, 'win_comment', true ) );
}
// Replace the quickedit action to call an additional expandedOpen function
add_filter( 'comment_row_actions', 'my_quick_edit_action', 10, 2 );
function my_quick_edit_action( $actions, $comment ) {
global $post;
$actions['quickedit'] = '<a onclick="commentReply.close();if( typeof(expandedOpen) == \'function\' ) expandedOpen('.$comment->comment_ID.');commentReply.open( \''.$comment->comment_ID.'\',\''.$post->ID.'\',\'edit\' );return false;" class="vim-q" title="'.esc_attr__( 'Quick Edit' ).'" href="#">' . __( 'Quick Edit' ) . '</a>';
return $actions;
}
// Add some javscript into the footer for the edit comments page
add_action('admin_footer-edit-comments.php', 'my_quick_edit_javascript');
function my_quick_edit_javascript() {
// Create nonce
$nonce = wp_create_nonce( 'my_cmeta' );
?>
<script type="text/javascript">
function expandedOpen(id) {
// Pull the data from the new column and pass that along to our new input
var mv = jQuery('tr#comment-'+id+' .column-winning_comment').text() || '';
jQuery('#comment-meta-mycustommeta').val(mv);
}
function saveCommentMeta() {
// Build the data object for the ajax action setup to update comment meta
var cid = jQuery('#comment_ID').val() || 0;
var mv = jQuery('#comment-meta-mycustommeta').val() || '';
var data = {
action: "update_winning_comment",
_ajax_nonce: "<?php echo $nonce; ?>",
comment_ID: cid,
comment_metaKey: 'win_comment',
comment_metaValue: mv
};
// Post data to ajax
jQuery.post( ajaxurl, data, function(response) {
if( 'success' != response )
return;
// Valid response, update the new table column(so you don't need to refresh)
jQuery('tr#comment-'+cid+' .column-winning_comment').text(mv);
// Close the quickedit
commentReply.close();
});
};
</script>
<?php
}
// Setup ajax callback for the new action
add_action( 'wp_ajax_update_winning_comment', 'update_winning_comment_ajax' );
function update_winning_comment_ajax() {
// Check nonce
check_ajax_referer( 'my_cmeta' );
// Check expected fields are there
foreach( array( 'comment_ID', 'comment_metaKey', 'comment_metaValue' ) as $field )
if( !isset( $_POST[$field] ) )
die;
// Validate ID
$commentID = absint( $_POST['comment_ID'] );
if( !$commentID )
die;
// Validate meta key
$commentKey = sanitize_title( $_POST['comment_metaKey'] );
if( empty( $commentKey ) )
die;
// Validate meta value(or empty)
$commentVal = sanitize_title( $_POST['comment_metaValue'] );
// Update meta
update_comment_meta( $commentID, $commentKey, $commentVal );
// Success response
echo 'success';// Echoed value is received as the response
// And die, expected behaviour for ajax actions
die;
}
// Run filter on the comment reply box(necessary to add new fields into the quickedit box)
add_filter( 'wp_comment_reply', 'my_quick_edit_menu', 10, 2 );
function my_quick_edit_menu($str, $input) {
extract( $input );
$table_row = true;
if( $mode == 'single' )
$wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
else
$wp_list_table = _get_list_table('WP_Comments_List_Table');
// Get editor string
ob_start();
$quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' );
wp_editor( '', 'replycontent', array( 'media_buttons' => false, 'tinymce' => false, 'quicktags' => $quicktags_settings, 'tabindex' => 104 ) );
$editorStr = ob_get_contents();
ob_end_clean();
// Get nonce string
ob_start();
wp_nonce_field( "replyto-comment", "_ajax_nonce-replyto-comment", false );
if ( current_user_can( "unfiltered_html" ) )
wp_nonce_field( "unfiltered-html-comment", "_wp_unfiltered_html_comment", false );
$nonceStr = ob_get_contents();
ob_end_clean();
$content="<form method="get" action="">";
if ( $table_row ) :
$content .= '<table style="display:none;"><tbody id="com-reply"><tr id="replyrow" style="display:none;"><td colspan="'.$wp_list_table->get_column_count().'" class="colspanchange">';
else :
$content .= '<div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">';
endif;
$content .= '
<div id="replyhead" style="display:none;"><h5>Reply to Comment</h5></div>
<div id="addhead" style="display:none;"><h5>Add new Comment</h5></div>
<div id="edithead" style="display:none;">';
$content .= '
<div class="inside">
<label for="author">Name</label>
<input type="text" name="newcomment_author" size="50" value="" tabindex="101" id="author" />
</div>
<div class="inside">
<label for="author-email">E-mail</label>
<input type="text" name="newcomment_author_email" size="50" value="" tabindex="102" id="author-email" />
</div>
<div class="inside">
<label for="author-url">URL</label>
<input type="text" id="author-url" name="newcomment_author_url" size="103" value="" tabindex="103" />
</div>';
// Add new quick edit fields
$content .= '
<div class="inside">
<label for="comment-meta-mycustommeta">Winner</label>
<input type="text" id="comment-meta-mycustommeta" name="comment-meta-mycustommeta" size="50" value="" tabindex="104" />
<a onclick="saveCommentMeta();" class="button-secondary save-mycustommeta" href="#">'.__( 'Save' ).'</a>
</div>
<div style="clear:both;"></div>
</div>';
// Add editor
$content .= "<div id='replycontainer'>\n";
$content .= $editorStr;
$content .= "</div>\n";
$content .= '
<p id="replysubmit" class="submit">
<a href="#comments-form" class="cancel button-secondary alignleft" tabindex="107">Cancel</a>
<a href="#comments-form" class="save button-primary alignright" tabindex="106">
<span id="addbtn" style="display:none;">Add Comment</span>
<span id="savebtn" style="display:none;">Update Comment</span>
<span id="replybtn" style="display:none;">Submit Reply</span></a>
<img class="waiting" style="display:none;" src="'.esc_url( admin_url( "images/wpspin_light.gif" ) ).'" alt="" />
<span class="error" style="display:none;"></span>
<br class="clear" />
</p>';
$content .= '
<input type="hidden" name="user_ID" id="user_ID" value="'.get_current_user_id().'" />
<input type="hidden" name="action" id="action" value="" />
<input type="hidden" name="comment_ID" id="comment_ID" value="" />
<input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
<input type="hidden" name="status" id="status" value="" />
<input type="hidden" name="position" id="position" value="'.$position.'" />
<input type="hidden" name="checkbox" id="checkbox" value="';
if ($checkbox) $content .= '1'; else $content .= '0';
$content .= "\" />\n";
$content .= '<input type="hidden" name="mode" id="mode" value="'.esc_attr( $mode ).'" />';
$content .= $nonceStr;
$content .="\n";
if ( $table_row ) :
$content .= '</td></tr></tbody></table>';
else :
$content .= '</div></div>';
endif;
$content .= "\n</form>\n";
return $content;
}
And should produce, something like this..
Comment Meta – Quickedit http://img802.imageshack.us/img802/1337/commentmetaexample.jpg
First time i’ve written code for the quick edit box on the comments page, so excuse any areas that could use improvement(there’s always room for it), i don’t have alot of time to spend going over the code and making refinements at the moment.
Good luck, and i hope that’s been helpful.
Oh, and quick mention for Shibashake’s guide to messing with the Quick Edit, i used some of his code to put this together, so definitely deserving of some link juice.
Shibashake – Expand the WordPress Comments Quick Edit Menu