将COMMENT_META添加到wp_COMMENT_REPLY

时间:2016-08-19 作者:user2806026

我为我的所有评论添加了一些自定义元数据。我在后端显示此元数据。但是,我希望能够在wp\\u comment\\u reply编辑器中插入元数据:

enter image description here

我想我可以使用wp\\u comment\\u reply过滤器来实现这一点,但我无法让它发挥作用。有人有主意吗?

这就是我所尝试的:

function wporg_more_comments( $content ) {
    echo \'123\';
    return $content;
}

add_filter( \'wp_comment_reply\', \'wporg_more_comments\', 5, 1 );

1 个回复
SO网友:Ignat B.

考虑到WP挂钩的功能,在这里设置额外内容的唯一方法是使用挂钩重新组织回复/编辑表单本身。

下面的示例演示如何在“回复”表单上添加额外的HTML内容。找出代码中的“这是您的自定义内容”标签,以检查魔法发生的地方。

add_filter(\'wp_comment_reply\', \'test_reply_comment_func\', 10, 2);

function test_reply_comment_func($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>\'
            . \'<p style="margin:10px 0;"> HERE IS YOUR CUSTOM CONTENT</p>\'
            . \'</div>\';


    $content .= \'<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>
                <div style="clear:both;"></div>\';

    $content .= \'</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="106">Cancel</a>
            <a href="#comments-form" class="save button-primary alignright" tabindex="104">
            <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;
}

Result

enter image description here

Exaplanation.

此代码覆盖默认答复表单代码。回复注释和编辑注释内部连接。当您触发“快速编辑”时<div> 属于.addhead.edithead 可见,.replyhead - 隐藏的当您触发“回复”时.addhead.edithead 是隐藏的,.replyhead - 看得见的