向WordPress评论添加另一个状态(垃圾邮件、拒绝、批准)?

时间:2013-04-28 作者:mathiregister

我知道这听起来有点奇怪,但我想做的很简单,但技术上可能很难。

我正在我的wordpress网站上举办小型比赛,请用户发表评论。我会在一段时间后选择一条评论,这就是赢家。

我现在想做的是,将该评论标记为赢家。

因此,我正在考虑这样的解决方案:在/wp admin中查看评论时,我可以将评论设置为垃圾邮件、已批准、已拒绝等。我希望有另一个链接/按钮,上面写着:“赢家”。如果我单击此链接,注释会有一个附加类,如<li class="comment odd alt thread-even depth-1 winner">

你有没有想到一个聪明的解决方案?我该怎么做?还有其他想法吗?

提前谢谢你。马特

1 个回复
最合适的回答,由SO网友:t31os 整理而成

假设您希望工具在查看注释列表时从快速编辑框中更新此数据,则需要一系列操作和过滤器。我试图在必要的地方为您做出适当的评论,但请记住,我通过少量测试为您提供了所有这些内容(尽管它确实有效)。

这应该给你一个很好的起点。。。

// 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;
}
应该会产生这样的结果。。

Comment Meta - Quickedit http://img802.imageshack.us/img802/1337/commentmetaexample.jpg

我第一次为评论页面上的快速编辑框编写代码,所以请原谅任何需要改进的地方(总是有改进的空间),我现在没有太多时间来检查代码并进行改进。

祝你好运,我希望这会有所帮助。

哦,很快提到Shibashake的快速编辑指南,我使用了他的一些代码来将其组合在一起,因此绝对值得一些链接。

Shibashake - Expand the WordPress Comments Quick Edit Menu

结束

相关推荐

如何使用‘COMMENTS_NUMBER’过滤器?

我不知道怎么用this filter. 有人能给我举个例子吗?我正在努力改变(\'0\', \'1\', \'%\') 到(\'0 Comments\', \'1 Comment\', \'% Comments\').我正在使用此函数获取评论编号。 function lpe_get_comments_number( $anchor=\'#comments\' ) { $return = \'<a class=\"comments-number-link\" href=\