php 如何自定义Wordpress comment_form();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11333810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize Wordpress comment_form();
提问by kaffolder
I am currently developing my own Wordpress theme and have been recently working on a custom comments_template();. I have read that using the wp_list_comments();method is best practice for pulling in and displaying the comments per page/post. I have successfully customized the way that the comments are pulled in through that method and displayed.
我目前正在开发我自己的 Wordpress 主题,并且最近一直致力于自定义comments_template();. 我已经读过,使用该wp_list_comments();方法是获取和显示每页/帖子的评论的最佳实践。我已经成功地自定义了通过该方法引入和显示评论的方式。
I have also read that using the comment_form();method is the best practice for displaying the comment form. However, I am really struggling with trying to customize this. I am a little confused between the $args, filtersand actions.
我还读到使用该comment_form();方法是显示评论表单的最佳实践。但是,我真的很难尝试自定义它。我在$args、filters和actions之间有点困惑。
Essentially I would like to drastically change parts of the comment form. How might I go about changing parts of the comment form while still using best practice with the comment_form();method?
本质上,我想彻底改变评论表单的部分内容。我如何在仍然使用该comment_form();方法的最佳实践的同时更改评论表单的部分内容?
All I am really needing to do is wrap several of the existing <p>tags in <divs>. List of updates I am trying to make are below:
我真正需要做的就是将几个现有<p>标签包装在<divs>. 我正在尝试进行的更新列表如下:
- Tweak the
<h3>header to<h2 class="comments-header">Tell us about you!</h2> - Wrap form fields in
<fieldset></fieldset> - Wrap
<label>in<div class="label"></div> - Wrap
<input>in<div class="field"></div> - Make
<p class="form-allowed-tags"></p>display beforethe comment<textarea>rather than after - Change form Submit button to use the
<button>element rather than<input>
- 将
<h3>标题调整为<h2 class="comments-header">Tell us about you!</h2> - 将表单字段包装在
<fieldset></fieldset> - 包裹
<label>起来<div class="label"></div> - 包裹
<input>起来<div class="field"></div> - 制作
<p class="form-allowed-tags"></p>显示之前的评论<textarea>,而不是之后 - 更改表单提交按钮以使用该
<button>元素而不是<input>
Please see the code below for further explanation...
请参阅下面的代码以获得进一步的解释...
Default comment_form(); code that is output:
默认comment_form(); 输出的代码:
<div id="respond">
<h3 id="reply-title">Leave a Reply</h3>
<form action="http://localhost/.../wp-comments-post.php" method="post" id="commentform">
<p class="comment-notes">
Your email address will not be published. Required fields are marked
<span class="required">*</span>
</p>
<p class="comment-form-author">
<label for="author">Name</label>
<span class="required">*</span>
<input id="author" name="author" type="text" value="John Doe" size="30" aria-required="true">
</p>
<p class="comment-form-email">
<label for="email">Email</label>
<span class="required">*</span>
<input id="email" name="email" type="text" value="[email protected]" size="30" aria-required="true">
</p>
<p class="comment-form-url">
<label for="url">Website</label>
<input id="url" name="url" type="text" value size="30">
</p>
<p class="comment-form-comment">
<label for="comment">Comment</label>
<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>
</p>
<p class="form-allowed-tags">
You may use these HTML tags and attributes...
</p>
<p class="form-submit">
<input name="submit" type="submit" id="submit" value="Post Comment">
<input type="hidden" name="comment_post_ID" value="22" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</p>
</form>
</div> <!-- #respond -->
Code I am trying to output:
我试图输出的代码:
<div id="respond">
<h2 class="comments-header">Tell us about you!</h2>
<form action="http://localhost/.../wp-comments-post.php" method="post" id="commentform">
<fieldset>
<div class="label"><label for="author">Name <span class="required">*</span></label></div>
<div class="field"><input id="author" name="author" type="text" value="<?php echo $comment_author_email; ?>" size="30" aria-required="true"></div>
</fieldset>
<fieldset>
<div class="label"><label for="email">E–mail (will not be published) <span class="required">*</span></label></div>
<div class="field"><input id="email" name="email" type="text" value="<?php echo $comment_author_email; ?>" size="30" aria-required="true"></div>
</fieldset>
<p class="form-allowed-tags">
You may use these HTML tags and attributes...
</p>
<fieldset>
<div class="field"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></div>
</fieldset>
<p class="form-submit">
<button class="story-submit-btn" type="submit" name="submit" id="sub">Post your story</button>
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</p>
</form>
</div> <!-- #respond -->
Any help is GREATLY appreciated!
任何帮助是极大的赞赏!
采纳答案by Tomek Buszewski
I use functions.phpto modify comments display. I don't know if it is the way things are done now (last site that I was developing with WP and needed comments was in 2009 ;)), but here it is (place it in functions.phpfile!:
我functions.php用来修改评论显示。我不知道这是否是现在做事的方式(我用 WP 开发的最后一个网站和需要的评论是在 2009 年;)),但它在这里(将它放在functions.php文件中!:
function THEMENAME_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
*your comment display code*
}
Remember to create pingback theme aswell. You do it similar to comments, only difference is the first line:
记得还要创建 pingback 主题。你这样做类似于评论,唯一的区别是第一行:
function THEMENAME_pings($comment, $args, $depth)
function THEMENAME_pings($comment, $args, $depth)
Other way may be using comments_template.
其他方式可能是使用comments_template。
回答by yitwail
If all else fails, in comments.php in your theme directory,
change comment_form($args);to
如果所有其他方法都失败了,请在您的主题目录中的 comments.php 中,更改comment_form($args);为
ob_start();
comment_form($args);
$comment_form = ob_get_clean();
//insert code to modify $comment_form
echo $comment_form;
I used this to change the submit button to an image button.
我用它来将提交按钮更改为图像按钮。
回答by krishna singh
Simple example how to change some comment form fields.
如何更改一些评论表单字段的简单示例。
$comments_args = array(
// change the title of send button
'label_submit'=>'Send',
// change the title of the reply section
'title_reply'=>'Write a Reply or Comment',
// remove "Text or HTML to be displayed after the set of comment fields"
'comment_notes_after' => '',
// redefine your own textarea (the comment body)
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);
comment_form($comments_args);
For more information: comment_form() documentation on WordPress Codex
回答by dmontooth
The comment_form uses an array of fields. The values in that array are a string. You can override the output of the fields by adding your own string for the respective key, like so:
comment_form 使用一个字段数组。该数组中的值是一个字符串。您可以通过为相应的键添加自己的字符串来覆盖字段的输出,如下所示:
function modify_comment_fields($fields){
$fields = array('author' =>'<div><p class="comment-form-author"><label for="author">Author</label><input id="author" name="author" type="text"/></p></div>');
return $fields;
}
Then add a filter to your function:
然后向您的函数添加过滤器:
add_filter('comment_form_default_fields','modify_comment_fields');
Here's a link to the Codex on the comment_form function: http://codex.wordpress.org/Function_Reference/comment_form
这是关于comment_form 函数的Codex 链接:http: //codex.wordpress.org/Function_Reference/comment_form
回答by krishna singh
On Single.php after post content
Use it for custom comment form html and design
<code> <?php $comments_args = array(
// change the title of send button
'label_submit'=>'Submit',
// change the title of the reply section
'title_reply'=>'Add a comment',
// remove "Text or HTML to be displayed after the set of comment fields"
'comment_form_top' => 'ds',
'comment_notes_before' => '',
'comment_notes_after' => '',
// redefine your own textarea (the comment body)
'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" placeholder="Your Comment* " aria-required="true"></textarea></p>',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' =>
'<p class="comment-form-author">' .
'<input id="author" class="blog-form-input" placeholder="Your Name* " name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
'" size="30"' . $aria_req . ' /></p>',
'email' =>
'<p class="comment-form-email">'.
'<input id="email" class="blog-form-input" placeholder="Your Email Address* " name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) .
'" size="30"' . $aria_req . ' /></p>',
'url' =>
'<p class="comment-form-url">'.
'<input id="url" class="blog-form-input" placeholder="Your Website URL" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
'" size="30" /></p>'
)
),
);
comment_form($comments_args);?></code>
回答by janw
Did you check the codex: http://codex.wordpress.org/Function_Reference/comment_form
你检查过代码:http: //codex.wordpress.org/Function_Reference/comment_form
It not easy to customize but it's doable
定制并不容易,但它是可行的
回答by ruoesh
function THEMENAME_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
*your comment display code*
}
回答by nikksan
My theme used the default function comment_form, so I made a change in the child theme.
我的主题使用了默认函数comment_form,所以我对子主题进行了更改。
if(function_exists('glutton_comment_form'))
glutton_comment_form(); //custom function for displaying comments, defined in glutton-child/functions.php
else
comment_form();

