添加和存储额外的字段-WordPress评论

时间:2016-10-28 作者:egr103

我使用的是标准的Wordpress评论表单,但我想在表单中添加单选按钮作为附加字段,如下所示:

enter image description here

我的PHP生成标准表单如下,但我不知道添加、存储和显示(在前端)附加信息的最佳途径:

<?php
$fields =  array(
\'author\' =>
\'<p class="comment-form-author"><label for="author">\' . __( \'Name\', \'domainreference\' ) . \'</label> \' .
( $req ? \'<span class="required">*</span>\' : \'\' ) .
\'<input id="author" name="author" placeholder="Enter your name" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',

\'email\' =>
\'<p class="comment-form-email"><label for="email">\' . __( \'Email\', \'domainreference\' ) . \'</label> \' .
( $req ? \'<span class="required">*</span>\' : \'\' ) .
\'<input id="email" name="email" placeholder="Enter your email address" type="text" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',

\'comment_field\' =>
  \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Comment\', \'noun\' ) .
\'</label><textarea id="comment" placeholder="Enter your comment" name="comment" cols="45" rows="8" aria-required="true">\' .
\'</textarea></p>\'
);

$args = array(
  \'id_form\'           => \'commentform\',
  \'class_form\'      => \'comment-form\',
  \'id_submit\'         => \'submit\',
  \'class_submit\'      => \'submit btn\',
  \'name_submit\'       => \'submit\',
  \'title_reply\'       => __( \'Leave a Reply\' ),
  \'title_reply_to\'    => __( \'Leave a Reply to %s\' ),
  \'cancel_reply_link\' => __( \'Cancel Reply\' ),
  \'label_submit\'      => __( \'Post Comment\' ),
  \'format\'            => \'xhtml\',

  \'fields\' => apply_filters( \'comment_form_default_fields\', $fields )

);

comment_form( $args ); ?>

1 个回复
最合适的回答,由SO网友:egr103 整理而成

事实证明,我已经在使用高级自定义字段插件,可以非常轻松地将其他字段插入到评论表单中。

ACF非常好,它创建了一个教程来实现这一点,以及如何在注释线程中输出数据:https://www.advancedcustomfields.com/resources/get-values-comment/

然而,通过这样做,插件在前端添加了一堆多余的CSS和JS文件,因此要删除这些文件,请将这一段代码添加到您的函数中。php:

// disable acf css on front-end acf forms
add_action( \'wp_print_styles\', \'my_deregister_styles\', 100 );

function my_deregister_styles() {
  wp_deregister_style( \'acf\' );
  wp_deregister_style( \'acf-field-group\' );
  wp_deregister_style( \'acf-global\' );
  wp_deregister_style( \'acf-input\' );
  wp_deregister_style( \'acf-datepicker\' );
}