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

参考主题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 文章置顶循环
下一篇

发表评论

注册不是必须的

像素鱼丸
160 文章
2 评论
4 喜欢
最新文章

VooShop 商城主题

主题简介 Vooshop 是一个简单精美的 WordPress 主题,支持自适应、暗黑模式、多语言等功能,可以用于企业、团体、自由职业者或更广泛的主页。 Vooshop:回归 WooCommerce 的纯粹与极速 零冗余代码,零多余功能。一个完全依赖 WooCommerce 原生能力的轻量级主题。 Vooshop 不做 WooCommerce 做不到的事,我们只负责让它跑得更快。 市面上的主题总试 […]

XZhan 主题

主题简介 XZhan 主题(中文名称:小站主题)是一款 WordPress 企业主题,支持自适应、暗黑模式等功能,可快速构建高质量的企业网站。   主题特色 支持白天与暗黑模式 自适应设计,兼容多种主流浏览器 自定义主色调 LOGO扫光动画 自定义SMTP支持 内置SEO功能 文章支持点赞、分享和海报生成 丰富的小工具 侧边栏粘性滚动 简介的主题设置面板 主题设置可导入和备份 多级子菜单 […]

StarFish 配置框架

一个轻量级的 WordPress 选项框架插件,通过配置化的方式,快速为 WordPress 主题或插件生成后台设置页面。 ✨ 特性 🎯 配置驱动 UI:只需定义数组配置,自动生成完整的表单界面 📱 多页面架构:支持多个独立的设置页面 🎨 丰富的字段类型:包含 15+ 种常用字段类型 🔗 字段依赖系统:实现字段间的联动效果 ✅ 数据验证与清理:自动进行安全清理,防止 XSS 攻击 🚀 零依赖:使用 […]

什么是幽灵按钮

“幽灵按钮”(Ghost Button)是一种常见的网页与移动应用 UI 设计模式,指背景透明(或半透明)、仅通过边框(border)和文字(text)定义的按钮,视觉上“若隐若现”,仿佛“幽灵”一般——因此得名。 核心特征: 无填充色(transparent background) 背景完全透明(或与父容器同色),不遮挡背后内容。 清晰的边框(通常 1–2px 实线) 如 border: 2px […]
生成中...
扫描二维码
扫描二维码
确认购买

您确定要购买此资源吗?

微信扫码支付

请使用微信扫描二维码完成支付

订单号:

等待支付...