WordPress 评论表单函数 comment_form()
像素鱼丸
2023-06-29
1306
0

参考主题WordPress自带主题, twentytwentyone,在文章模版下面,有个函数 :

<?php comments_template(); ?>

这个函数会引用主题下的 comments.php,在该文件的底部有个函数 comment_form 函数,就是用来生成表单的。

//Declare Vars
$comment_send = 'Send';
$comment_reply = 'Leave a Message';
$comment_reply_to = 'Reply';
 
$comment_author = 'Name';
$comment_email = 'E-Mail';
$comment_body = 'Comment';
$comment_url = 'Website';
$comment_cookies_1 = ' By commenting you accept the';
$comment_cookies_2 = ' Privacy Policy';
 
$comment_before = 'Registration isn\'t required.';
 
$comment_cancel = 'Cancel Reply';
 
//Array
$comments_args = array(
    //Define Fields
    'fields' => array(
        //Author field
        'author' => '<p class="comment-form-author"><br /><input id="author" name="author" aria-required="true" placeholder="' . $comment_author .'"></input></p>',
        //Email Field
        'email' => '<p class="comment-form-email"><br /><input id="email" name="email" placeholder="' . $comment_email .'"></input></p>',
        //URL Field
        'url' => '<p class="comment-form-url"><br /><input id="url" name="url" placeholder="' . $comment_url .'"></input></p>',
        //Cookies
        'cookies' => '<input type="checkbox" required>' . $comment_cookies_1 . '<a href="' . get_privacy_policy_url() . '">' . $comment_cookies_2 . '</a>',
    ),
    // Change the title of send button
    'label_submit' => __( $comment_send ),
    // Change the title of the reply section
    'title_reply' => __( $comment_reply ),
    // Change the title of the reply section
    'title_reply_to' => __( $comment_reply_to ),
    //Cancel Reply Text
    'cancel_reply_link' => __( $comment_cancel ),
    // Redefine your own textarea (the comment body).
    'comment_field' => '<p class="comment-form-comment"><br /><textarea id="comment" name="comment" aria-required="true" placeholder="' . $comment_body .'"></textarea></p>',
    //Message Before Comment
    'comment_notes_before' => __( $comment_before),
    // Remove "Text or HTML to be displayed after the set of comment fields".
    'comment_notes_after' => '',
    //Submit Button ID
    'id_submit' => __( 'comment-submit' ),
);
comment_form( $comments_args );

 

另外列出一些,评论表单钩子:

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 中添加:

function add_some_text() {
    echo '欢迎欢迎';
}
// 在默认字段(前面说的姓名、邮箱和网址)的下面添加字段
add_filter('comment_form_after_fields', 'add_some_text');
// 在已登录下面添加字段(因为用户登录后,是没有默认上面三个字段的),所以要使用这个钩子插入内容
add_filter('comment_form_logged_in_after', 'add_some_text');

参考:https://developer.wordpress.org/reference/functions/comment_form/

收藏
打赏
get_categories() 获取所有分类
上一篇
WordPress 文章置顶循环
下一篇

发表评论

注册不是必须的

像素鱼丸
152 文章
0 评论
4 喜欢
最新文章

网站的 Cookie 弹窗

在当前的法规环境下(截至2026年4月),一个合规的Cookie弹窗设计必须遵循“透明、公平、明确”的原则,核心是确保用户拥有真正的选择权。 以下是现阶段设计合规Cookie弹窗的关键要点: 现阶段合规设计要点 禁止默认同意 弹窗出现时,所有非必要Cookie的选项都不能被预先勾选。用户必须通过一个明确的、主动的动作(如点击按钮或勾选方框)来表示同意。 提供平等的选择权 “拒绝”按钮必须在视觉上和 […]

网页设计中 banner、jumbotron、hero 都有什么区别

在网页设计和开发中,banner、jumbotron、hero 这些词通常指代页面顶部最引人注目的区域,但它们在具体含义和使用场景上有所区别。 🎯 核心概念辨析 Banner (横幅/条幅广告) 这是一个非常广泛的术语,通常指网页上任何矩形的广告或信息区域,可以出现在页面的顶部、侧边或底部。它更偏向于广告或信息展示的功能。 Jumbotron (巨幕) 这个词源于 Bootstrap 等前端框架, […]

宝塔 Nginx 拦截了 WordPress 返回 404 的状态

REST api 接口中,有段代码返回404,但是没有正常返回错误json: $wp_response = new \WP_REST_Response(array('error'=>'没有找到数据')); $wp_response->set_status(404); return $wp_response; 返回内容是: <html> <head><tit […]

Mirage 主题 v2.93.0 发布

更新内容: refactor 移除图片高宽比开关 refactor 移除全局的TOC生成开关 refactor 优化 header.php 中的seo模块和样式覆盖 feat 主题启用的时候,移除非当前主题注册的小工具 fix 修复分类小工具的bug feat 管理员打开后台,检查最当前设置首页布局的模块,如果缺少最新模块,就添加到隐藏模块列表中 fix 优化链接卡片样式 fix 优化热门文章样式 […]
生成中...
扫描二维码
扫描二维码
用户登录