WordPress 评论表单函数 comment_form()
像素鱼丸
2023-06-29
1247
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 文章置顶循环
下一篇

发表评论

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

详解 WordPress 的评论设置

好的,我们来详细梳理并总结 WordPress 中关于文章评论的两个核心控制层级:全局设置和单篇设置。理解这两者的关系(优先级)是管理网站评论的关键。 1. 全局设置 (Global Settings) —— 网站的“默认规则” 这是整个网站评论系统的总开关和默认行为准则。它决定了新发布的文章默认是什么样子的。 位置:WordPress 后台仪表盘 -> 设置 (Settings) -> […]

wp_handle_comment_submission 函数

wp_handle_comment_submission() 是 WordPress 中用于处理评论提交的核心函数之一。它通常在用户提交评论时被调用,负责验证和处理评论数据,并最终将评论插入到数据库中。 ✅ 函数作用 wp_handle_comment_submission() 的主要功能是: 验证用户提交的评论数据(如评论内容、用户名、邮箱等) 检查是否为垃圾评论(通过 Akismet 或其他过 […]

Mirage 主题 v2.89.0 发布

Mirage 主题 v2.89.0 发布 feat 懒加载图片增加灰色背景颜色 refactor 移除一些无用的js文件 fix 优化卡片列表样式 feat 如果设置里删除logo,就直接显示网站名称 fix 修复pc下拉菜单宽度的一个样式bug fix 修复未登录用户取消喜欢时产生的一个错误 fix 优化 wp_vt_star 表结构 下载地址:https://gitee.com/vthemec […]

WordPress set_transient()使用方法和实现

set_transient() 是 WordPress 中用于设置临时数据的函数,它允许你将数据存储在缓存中,这些数据会在指定的时间后自动过期。它是 WordPress 提供的 transient API 的一部分,常用于缓存数据库查询、API 响应或其他计算密集型操作的结果。 一、基本概念 1. 什么是 Transient? Transient 是 WordPress 中的一种缓存机制,类似于 […]
生成中...
扫描二维码
扫描二维码
用户登录