php 自定义wordpress评论表单html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19973089/
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
custom wordpress comment form html
提问by LordZardeck
I would like to modify the actual HTML of the comment forms in WordPress. I know of the comment_form()
function that WordPress provides, but it doesn't actually let me modify the html of the form. What I'm specifically trying to do is make a bootstrap responsive form, which is not possible with the options provided with the comment_form()
function. I've tried looking all over the internet for a way, but to no avail. Can anyone point me in the right direction?
我想修改 WordPress 中评论表单的实际 HTML。我知道comment_form()
WordPress 提供的功能,但它实际上并没有让我修改表单的 html。我特别想做的是制作一个引导响应式表单,这对于该comment_form()
功能提供的选项是不可能的。我试过在互联网上寻找一种方法,但无济于事。任何人都可以指出我正确的方向吗?
Let me clarify this:
让我澄清一下:
I am looking to modify the actual FORM and containing DIV elements of the comment form, not just the fields.
我希望修改实际的 FORM 并包含评论表单的 DIV 元素,而不仅仅是字段。
回答by lukejanicke
The uncustomizable parts of the function comment_form()
should (idealistically) be limited purely to code that is essential for security and proper form handling by the WordPress core. However, that is not the case. To explore the issue, I used filters (could have used arguments) to completely remove all the code that is customizable. I set all available variables to empty strings—that is, all the stuff mentioned on the codex page for comment_form()
.
该函数的comment_form()
不可定制部分应该(理想情况下)仅限于对安全性和 WordPress 核心正确处理表单至关重要的代码。然而,事实并非如此。为了探索这个问题,我使用过滤器(可以使用参数)来完全删除所有可自定义的代码。我将所有可用变量设置为空字符串——也就是说,所有在comment_form()
.
Here is the left-over, non-customisable code from comment_form()
:
这是来自 的剩余的、不可定制的代码comment_form()
:
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title"> <small><a rel="nofollow" id="cancel-comment-reply-link" href="/1#respond" style="display:none;">Click here to cancel reply.</a></small></h3>
<form action="http://www.[yourdomain].com/wp-comments-post.php" method="post" id="" class="comment-form">
<p class="form-submit">
<input name="submit" type="submit" id="" value="" />
<input type='hidden' name='comment_post_ID' value='1' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p>
</form>
</div><!-- #respond -->
Where does all this come from? At the bottom of the codex page for comment_form()
it says…
这一切从何而来?在comment_form()
它的法典页面底部说……
Source Code
comment_form()
is located in wp-includes/comment-template.php.
源代码
comment_form()
位于wp-includes/comment-template.php。
comment-template.php
is linked and you can view the full code in the browser. comment_form()
starts on line 1960. In fact, it's the last function in that file. There are no arguments or filters that let you modify the residual code above. These lines of code are all moreorless “hard-coded”.
comment-template.php
已链接,您可以在浏览器中查看完整代码。comment_form()
从 1960 行开始。实际上,它是该文件中的最后一个函数。没有参数或过滤器可以让您修改上面的残留代码。这些代码行都是或多或少的“硬编码”。
The text Click here to cancel reply.is the only text that survived my filter genocide. Strangely, comment_form()
has back-up text for cancel_reply_link
hard-coded into the function, in case it is passed to the function as an empty string. None of the other filterable items have hard-coded back-ups.
文字点击此处取消回复。是唯一在我的过滤器种族灭绝中幸存下来的文本。奇怪的是,comment_form()
具有cancel_reply_link
硬编码到函数中的备份文本,以防它作为空字符串传递给函数。其他可过滤项目都没有硬编码备份。
It is easy to see which bits of code are essential and which bits are non-essential for a HTML form. A little more difficult to judge is which bits are essential for a WordPressform. Some of the bits above were dynamically output (note that this is the first comment on a development blog with no other replies, no parent/child comments).
很容易看出哪些代码位对于 HTML 表单是必不可少的,哪些位是非必要的。更难判断的是哪些位对于WordPress表单是必不可少的。上面的一些位是动态输出的(请注意,这是开发博客上的第一条评论,没有其他回复,没有父/子评论)。
From the comment_form()
function in comment-template.php
you can draw out the code needed to produce the dynamic parts of the WordPress form. Then, with default arguments taken from the codex page for comment_form()
, you could piece together a barebones form, hard-coding the desired fields, labels and wrapping HTML. I'm doing that now and putting my custom form function in my theme's comments.php
template file, which I call using comments_template()
only in single.php
(for this particular theme).
您可以从 中的comment_form()
函数中comment-template.php
提取生成 WordPress 表单动态部分所需的代码。然后,使用从 codex 页面获取的默认参数 for comment_form()
,您可以拼凑一个准系统表单,对所需的字段、标签和包装 HTML 进行硬编码。我现在正在这样做并将我的自定义表单函数放在我的主题comments.php
模板文件中,我comments_template()
只调用使用in single.php
(对于这个特定主题)。
The result would be a full and proper, lean and mean WordPress comment form. But… it would be a form that could not be customized anymore using comment_form()
arguments or related filters unless you went ahead and included the filter code in your own comment form function. Since you're customizing the heck out it already, that's probably not a problem. Similarly, all the WordPress actions would also be unavailable to you or any other plugins unless you also triggered those action functions in your own comment form function. Again, probably not an issue for you at this point.
结果将是一个完整而恰当的、精益的和刻薄的 WordPress 评论表。但是……这将是一个无法再使用comment_form()
参数或相关过滤器自定义的表单,除非您继续将过滤器代码包含在您自己的评论表单函数中。由于您已经对其进行了自定义,因此这可能不是问题。同样,除非您还在自己的评论表单功能中触发了这些操作功能,否则您或任何其他插件也无法使用所有 WordPress 操作。同样,此时对您来说可能不是问题。
But most importantly, the resulting form might break your theme if future WordPress updates change the way the core handles forms.
但最重要的是,如果未来的 WordPress 更新改变了核心处理表单的方式,生成的表单可能会破坏您的主题。
If you're aware of those risks, you can probably rebuild a hand-coded comment form just from copying code on the codex page for comment_form()
and in wp-includes/comment-template.php. I don't have the finished code, otherwise I'd post it. But I will post when/if I succeed.
如果您意识到这些风险,您可能可以通过复制代码页面comment_form()
上wp-includes/comment-template.php和wp-includes/comment-template.php 中的代码来重建手动编码的评论表单。我没有完成的代码,否则我会发布它。但我会在/如果我成功时发布。
Right, that's all from me for now. Bear in mind (all readers) that despite appearances, I am an amateur WordPress theme developer and my proficiency with PHP and WordPress is very rudimentary. Just writing up this post I learned a lot.
对,这就是我现在的全部。请记住(所有读者),尽管出现,我是一名业余 WordPress 主题开发人员,我对 PHP 和 WordPress 的熟练程度非常初级。刚刚写这篇文章,我学到了很多。
I'm also worried that a full and proper solution is already out there somewhere but I haven't found it in my searches.
我还担心某个地方已经有一个完整而适当的解决方案,但我在搜索中没有找到它。
回答by Sashi
Just Create a New PHP file with the name of comment_form.php then paste all of your code. And your comment form is ready.
只需创建一个名为 comment_form.php 的新 PHP 文件,然后粘贴所有代码。您的意见表已准备就绪。
you shouldn't edit the wp-includes/comment-template.php file.
你不应该编辑 wp-includes/comment-template.php 文件。
Keep this for your ref for more styling your existing comment form
保留此作为您的参考,以获得更多样式您现有的评论表单
http://codex.wordpress.org/Template_Tags/comment_form
http://codex.wordpress.org/Template_Tags/comment_form
Or this one
或者这个
http://wordpress.org/support/topic/where-to-find-the-comment_form-function-in-wp3
http://wordpress.org/support/topic/where-to-find-the-comment_form-function-in-wp3
Form code and action
表单代码和操作
<div class="comment-form">
<h2 class="comments-wrapper-heading"> Leave a comment </h2>
<form id="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<div class="commentform-element">
<label class="hide" for="author">Full Name</label>
<input class="input-fields" id="author" name="author" type="text" placeholder="Full Name" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="author">Email</label>
<input class="input-fields" id="email" name="email" type="text" placeholder="Email" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="comment">Message</label>
<textarea id="comment" class="input-fields" placeholder="Message" name="comment" cols="40" rows="200"></textarea>
</div>
<input name="submit" class="form-submit-button" type="submit" id="submit-comment" 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">
</form>
</div>
回答by Fabien Snauwaert
WP does not offer a way to create and use a comment formtemplate. i.e.: as of 4.1.1, there's still no way to control allof the HTML markup that makes up the form. There are only ways to edit partsof it and that's by supplying arguments to comment_form()
.
WP 不提供创建和使用评论表单模板的方法。即:从 4.1.1 开始,仍然无法控制构成表单的所有HTML 标记。只有方法来编辑部分通过向供应论证它,这就是comment_form()
。
So, if you would like to edit the whole layout (e.g.: place some of the content side to side instead of on top of one another; replace some of the <p>
with <div>
), you're out of luck.
所以,如果你想编辑整个布局(如:将一些内容方到另一侧,而不是在彼此的顶部,更换一些<p>
有<div>
),你的运气了。
What can we do?
我们可以做什么?
An option would be to borrow the whole comment_form()
function from the WordPress codebase (like this guy did): make a copy of the function in your theme, rename it, customize it and then call it in place of the vanilla function. That will work but there's a chance it might break on a future WordPress update if some of the inner workings of WP change.
一个选择是comment_form()
从 WordPress 代码库中借用整个函数(就像这个人所做的那样):在你的主题中复制一个函数,重命名它,自定义它,然后调用它来代替 vanilla 函数。这会起作用,但如果 WP 的某些内部工作发生变化,它可能会在未来的 WordPress 更新中中断。
回答by Pieter Goosen
回答by Theox
Did you look for the comment_form() function in Wordpress code to see if you can edit it ?
您是否在 Wordpress 代码中查找了 comment_form() 函数以查看是否可以对其进行编辑?
You'll also find some informations here :
您还可以在这里找到一些信息:
回答by Sankalp Mishra
This function uses the default form. You need to make a comment-template.php
file in your theme and in that file you can make your form.
Now when you will call comments_template()
you will get your made form instead of default form.
该函数使用默认形式。您需要comment-template.php
在主题中创建一个文件,然后在该文件中创建表单。现在当你打电话时,comments_template()
你会得到你制作的表格而不是默认表格。
回答by this_guy
I don't think you can change the containing div (<div id="respond" class="comment-respond">
). Have a look at the comment-template.php
我认为您不能更改包含的 div ( <div id="respond" class="comment-respond">
)。看看comment-template.php
The only thing you can change is the form ID. 'id_form' => 'commentform'
您唯一可以更改的是表单 ID。 'id_form' => 'commentform'
You can make a responsive form without changing these elements.
您可以在不更改这些元素的情况下制作响应式表单。