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