自定义 WordPress 评论表单

323次阅读
没有评论

自定义

调用 comment_form()

如果你要在主题中调用评论表单,只需要在使用下面简单的代码即可:

<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">comment_form</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>

就像我们在官方的主题 twentyfourteen 的 comments.php 文件的倒数第2行看到一样:

<span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token comment">/**
 * The template for displaying Comments
 *
 * The area of the page that contains comments and the comment form.
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */</span>
<span class="token comment">/*
 * If the current post is protected by a password and the visitor has not yet
 * entered the password we will return early without loading the comments.
 */</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">post_password_required</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token delimiter important">?></span></span>
<span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comments<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comments-area<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">have_comments</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"><</span>h2 <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comments-title<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
		<span class="token php language-php"><span class="token delimiter important"><?php</span>
			<span class="token function">printf</span><span class="token punctuation">(</span> <span class="token function">_n</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'One thought on “%2$s”'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'%1$s thoughts on “%2$s”'</span><span class="token punctuation">,</span> <span class="token function">get_comments_number</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
				<span class="token function">number_format_i18n</span><span class="token punctuation">(</span> <span class="token function">get_comments_number</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">get_the_title</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"></</span>h2<span class="token punctuation">></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">get_comment_pages_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&&</span> <span class="token function">get_option</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'page_comments'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"><</span>nav <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comment-nav-above<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>navigation comment-navigation<span class="token punctuation">"</span></span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>navigation<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>h1 <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>screen-reader-text<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">_e</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Comment navigation'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>h1<span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>nav-previous<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">previous_comments_link</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'← Older Comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>nav-next<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">next_comments_link</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Newer Comments →'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span>
	<span class="token tag"><span class="token punctuation"></</span>nav<span class="token punctuation">></span></span><span class="token comment"><!-- #comment-nav-above --></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">endif</span><span class="token punctuation">;</span> <span class="token comment">// Check for comment navigation. </span><span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"><</span>ol <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comment-list<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
		<span class="token php language-php"><span class="token delimiter important"><?php</span>
			<span class="token function">wp_list_comments</span><span class="token punctuation">(</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
				<span class="token string single-quoted-string">'style'</span>      <span class="token operator">=></span> <span class="token string single-quoted-string">'ol'</span><span class="token punctuation">,</span>
				<span class="token string single-quoted-string">'short_ping'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
				<span class="token string single-quoted-string">'avatar_size'</span><span class="token operator">=></span> <span class="token number">34</span><span class="token punctuation">,</span>
			<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"></</span>ol<span class="token punctuation">></span></span><span class="token comment"><!-- .comment-list --></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">get_comment_pages_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">&&</span> <span class="token function">get_option</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'page_comments'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"><</span>nav <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>comment-nav-below<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>navigation comment-navigation<span class="token punctuation">"</span></span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>navigation<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>h1 <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>screen-reader-text<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">_e</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Comment navigation'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>h1<span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>nav-previous<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">previous_comments_link</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'← Older Comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span>
		<span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>nav-next<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">next_comments_link</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Newer Comments →'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span>
	<span class="token tag"><span class="token punctuation"></</span>nav<span class="token punctuation">></span></span><span class="token comment"><!-- #comment-nav-below --></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">endif</span><span class="token punctuation">;</span> <span class="token comment">// Check for comment navigation. </span><span class="token delimiter important">?></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token operator">!</span> <span class="token function">comments_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token delimiter important">?></span></span>
	<span class="token tag"><span class="token punctuation"><</span>p <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>no-comments<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">_e</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Comments are closed.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'twentyfourteen'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token tag"><span class="token punctuation"></</span>p<span class="token punctuation">></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">endif</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">endif</span><span class="token punctuation">;</span> <span class="token comment">// have_comments() </span><span class="token delimiter important">?></span></span>
	<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">comment_form</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>
<span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span><span class="token comment"><!-- #comments --></span>

comment_form() 参数

<span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token function">comment_form</span><span class="token punctuation">(</span><span class="token variable">$args</span><span class="token punctuation">,</span> <span class="token variable">$post_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span>
  • $args:comment_form() 的输出配置参数,为一个关联数组,配置项非常丰富,下面我们会详细说明。
  • $post_id:文章id,默认为空,即当前id
  • $args的默认配置:
<span class="token variable">$defaults</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
        <span class="token string single-quoted-string">'fields'</span>               <span class="token operator">=></span> <span class="token function">apply_filters</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'comment_form_default_fields'</span><span class="token punctuation">,</span> <span class="token variable">$fields</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'comment_field'</span>        <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="comment-form-comment"><label for="comment">'</span> <span class="token operator">.</span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Comment'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'noun'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'must_log_in'</span>          <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="must-log-in">'</span> <span class="token operator">.</span>  <span class="token function">sprintf</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'You must be <a href="%s">logged in</a> to post a comment.'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">wp_login_url</span><span class="token punctuation">(</span> <span class="token function">apply_filters</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'the_permalink'</span><span class="token punctuation">,</span> <span class="token function">get_permalink</span><span class="token punctuation">(</span> <span class="token variable">$post_id</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</p>'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'logged_in_as'</span>         <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="logged-in-as">'</span> <span class="token operator">.</span> <span class="token function">sprintf</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">admin_url</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'profile.php'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$user_identity</span><span class="token punctuation">,</span> <span class="token function">wp_logout_url</span><span class="token punctuation">(</span> <span class="token function">apply_filters</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'the_permalink'</span><span class="token punctuation">,</span> <span class="token function">get_permalink</span><span class="token punctuation">(</span> <span class="token variable">$post_id</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</p>'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'comment_notes_before'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="comment-notes">'</span> <span class="token operator">.</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Your email address will not be published.'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token punctuation">(</span> <span class="token variable">$req</span> <span class="token operator">?</span> <span class="token variable">$required_text</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">''</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</p>'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'comment_notes_after'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="form-allowed-tags">'</span> <span class="token operator">.</span> <span class="token function">sprintf</span><span class="token punctuation">(</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' <code>'</span> <span class="token operator">.</span> <span class="token function">allowed_tags</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</code>'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</p>'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'id_form'</span>              <span class="token operator">=></span> <span class="token string single-quoted-string">'commentform'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'id_submit'</span>            <span class="token operator">=></span> <span class="token string single-quoted-string">'submit'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'title_reply'</span>          <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Leave a Reply'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'title_reply_to'</span>       <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Leave a Reply to %s'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'cancel_reply_link'</span>    <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Cancel reply'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'label_submit'</span>         <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Post Comment'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>

自定义评论表单

删除表单字段

如果我们想要删除网址字段,只需要打开主题的 functions.php 文件,添加以下代码:

<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_form_default_fields'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'mytheme_remove_url'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
<span class="token keyword">function</span> <span class="token function">mytheme_remove_url</span><span class="token punctuation">(</span><span class="token variable">$arg</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$arg</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token variable">$arg</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

保存后刷新页面,你就会看到“url”输入框已经不存在了。

新增表单字段

假设我们要添加一个 QQ 字段,同样在主题的 functions.php 添加下面的代码即可:

<span class="token keyword">function</span> <span class="token function">my_fields</span><span class="token punctuation">(</span><span class="token variable">$fields</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token variable">$fields</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'qq'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'<p class="comment-form-qq">'</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'<label for="qq">'</span><span class="token operator">.</span><span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'QQ'</span><span class="token punctuation">)</span><span class="token operator">.</span><span class="token string single-quoted-string">'</label> '</span> <span class="token operator">.</span>
	<span class="token string single-quoted-string">'<input id="qq" name="qq" type="text" value="'</span> <span class="token operator">.</span> <span class="token function">esc_attr</span><span class="token punctuation">(</span> <span class="token variable">$commenter</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comment_qq'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'" size="30" /></p>'</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token variable">$fields</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_form_default_fields'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'my_fields'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

刷新页面,即可看到新增的表单。

替换默认表单字段

代码和上面的例子差不多,如果你设置的字段为(author、email、url)其中之一,即 $fields[‘author’]、$fields[’email’]、$fields[‘url’] ,就可以替换默认的字段的输出内容。

默认的这三个字段如下:

<span class="token variable">$fields</span> <span class="token operator">=</span>  <span class="token keyword">array</span><span class="token punctuation">(</span>
	<span class="token string single-quoted-string">'author'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="comment-form-author">'</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'<label for="author">'</span> <span class="token operator">.</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Name'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</label> '</span> <span class="token operator">.</span> <span class="token punctuation">(</span> <span class="token variable">$req</span> <span class="token operator">?</span> <span class="token string single-quoted-string">'<span class="required">*</span>'</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">''</span> <span class="token punctuation">)</span> <span class="token operator">.</span>
	<span class="token string single-quoted-string">'<input id="author" name="author" type="text" value="'</span> <span class="token operator">.</span> <span class="token function">esc_attr</span><span class="token punctuation">(</span> <span class="token variable">$commenter</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comment_author'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'" size="30"'</span> <span class="token operator">.</span> <span class="token variable">$aria_req</span> <span class="token operator">.</span> <span class="token string single-quoted-string">' /></p>'</span><span class="token punctuation">,</span>
	<span class="token string single-quoted-string">'email'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="comment-form-email"><label for="email">'</span> <span class="token operator">.</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Email'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</label> '</span> <span class="token operator">.</span> <span class="token punctuation">(</span> <span class="token variable">$req</span> <span class="token operator">?</span> <span class="token string single-quoted-string">'<span class="required">*</span>'</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">''</span> <span class="token punctuation">)</span> <span class="token operator">.</span>
	<span class="token string single-quoted-string">'<input id="email" name="email" type="text" value="'</span> <span class="token operator">.</span> <span class="token function">esc_attr</span><span class="token punctuation">(</span>  <span class="token variable">$commenter</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comment_author_email'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'" size="30"'</span> <span class="token operator">.</span> <span class="token variable">$aria_req</span> <span class="token operator">.</span> <span class="token string single-quoted-string">' /></p>'</span><span class="token punctuation">,</span>
	<span class="token string single-quoted-string">'url'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'<p class="comment-form-url"><label for="url">'</span> <span class="token operator">.</span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Website'</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'</label>'</span> <span class="token operator">.</span>
	<span class="token string single-quoted-string">'<input id="url" name="url" type="text" value="'</span> <span class="token operator">.</span> <span class="token function">esc_attr</span><span class="token punctuation">(</span> <span class="token variable">$commenter</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'comment_author_url'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'" size="30" /></p>'</span><span class="token punctuation">,</span>
	<span class="token punctuation">)</span><span class="token punctuation">;</span>

comment_form() 钩子

评论表单同时还带了不少钩子,让你可以在喜欢的位置添加你想要的内容,具体钩子如下:

  • comment_form_before
  • comment_form_must_log_in_after
  • comment_form_top
  • comment_form_logged_in_after
  • comment_notes_before
  • comment_form_before_fields
  • comment_form_field_{$name}
  • comment_form_after_fields
  • comment_form_field_comment
  • comment_form (action hook)
  • comment_form_after
  • comment_form_comments_closed

在这里,倡萌只简单举一个小例子,在默认字段后面显示一句话,同样添加到主题的 functions.php :

<span class="token keyword">function</span> <span class="token function">add_my_tips</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">echo</span> <span class="token string single-quoted-string">'欢迎踊跃发言!'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 在默认字段(前面说的姓名、邮箱和网址)的下面添加字段</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_form_after_fields'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'add_my_tips'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 在已登录下面添加字段(因为用户登录后,是没有默认上面三个字段的),所以要使用这个钩子插入内容</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_form_logged_in_after'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'add_my_tips'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

其他的就靠大家多多实践了。

更多信息,请参考官方文档:http://codex.wordpress.org/Function_Reference/comment_form

 

迷鹿, lostfawn
版权声明:本站原创文章,由迷鹿, lostfawn2021-01-01发表,共计5472字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...
召唤伊斯特瓦尔