I already had this script working, but since the last update of the wordpress core it doesn't save the comment-meta anymore.
/**
* Comments Winner
*/
// Add new heading to comments tables
add_filter( 'manage_edit-comments_columns', 'winner_comment_column' );
function winner_comment_column( $columns ) {
$columns['winning_comment'] = __( '(lieb)gewonnen' );
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, 'winning_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', 'edit_winning_comment_ajax');
function edit_winning_comment_ajax() {
// 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() || '';
if ( mv == "gewonnen" ) {
jQuery('#comment-meta-winner').attr('checked', 'checked');
} else {
jQuery('#comment-meta-winner').removeAttr('checked');
}
}
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-winner').val() || '';
var data = {
action: "update_winning_comment",
_ajax_nonce: "<?php echo $nonce; ?>",
comment_ID: cid,
comment_metaKey: 'winning_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', 'wr_editComment_menu', 10, 2 );
function wr_editComment_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" style="margin:0 5px;">Name</label>
<input type="text" name="newcomment_author" size="50" value="" tabindex="101" id="author" />
</div>
<div class="inside">
<label for="author-email" style="margin:0 5px;">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" style="margin:0 5px;">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-winner" style="margin:0 5px; position:relative; top:2px;">Gewonnen</label>
<input type="checkbox" id="comment-meta-winner" name="comment-meta-winner" value="gewonnen" style="width:auto; margin-top:5px;" />
<!--<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 onclick="saveCommentMeta();" 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;
}
?>
Does anyone have an idea why this wouldn't work. The additinal checkbox in the comments-menu appears, however when trying to save it, it doesn't work. The comment-meta doesn't get stored in the db.
Kind Regards, Matt
update_comment_meta()
returns?saveCommentMeta()
is not defined? This is my problem in the first place.admin_footer-edit-comments.php
with for exampleadmin_footer
, to test if the action is too restrict for your setup.saveCommentMeta()
is called if I try and add aconsole.log(test)
to it. So when clicking on update in the comments-quickedit the test appears in my console. However nothing is saved.update_winning_comment_ajax()
), assignupdate_comment_meta( $commentID, $commentKey, $commentVal );
to a variable and return it's value. Subsequentlyconsole.log()
it out to see what it is. At least that way we can determine if it's failing on the update and work our way backwards