添加并存储额外字段 - Wordpress 评论

问题描述 投票:0回答:3

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

我生成标准表单的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 ); ?>
php wordpress comments custom-fields
3个回答
0
投票

添加并存储额外字段 - Wordpress 评论

// Add the fields to comment form
add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
function change_comment_form_defaults( $default ) {
     $commenter = wp_get_current_commenter();
     $default[ 'comment_field' ] .= '<p class="comment-form-author">' .
                                        '<label for="vote-type">'. __('How are you like to vote?') . '</label>
                                        <span class="required">*</span>
                                        <input id="vote-type-yes" name="vote-type" size="30" type="radio" value="Yes" />
                                        <label for="vote-type-yes">Yes</label>
                                        <input id="vote-type-no" name="vote-type" size="30" type="radio" value="No" />
                                        <label for="vote-type-no">No</label>
                                        <input id="vote-type-maybe" name="vote-type" size="30" type="radio" value="No" />
                                        <label for="vote-type-maybe">May Be</label>
                                     </p>';
            return $default;
}

// validations
add_filter( 'pre_comment_on_post', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
      if ( ! isset( $_POST['vote-type'] ) )
         wp_die( __( 'Error: please fill the required field (How are you like to vote?).' ) );
      return $commentdata;
}

// And save the comment meta
add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
      add_comment_meta( $comment_id, 'vote-type', $_POST[ 'vote-type' ] );
}


// Retrieve and display comment meta
add_filter( 'get_comment_author_link', 'attach_vote_type_to_author' );
function attach_vote_type_to_author( $author ) {
       $vote_type = get_comment_meta( get_comment_ID(), 'vote-type', true );
       if ( $vote_type )
            $author .= " ($vote_type)";
       return $author;
}

0
投票

事实证明,高级自定义字段(我已经在使用的一个插件)使得在评论表单中插入其他字段变得非常容易。

ACF 非常好,它创建了一个教程来执行此操作以及如何在评论线程中输出数据:https://www.advancedcustomfields.com/resources/get-values-comment/

但是,通过这样做,插件会在前端添加一堆多余的 CSS 和 JS 文件,因此要删除这些文件,请将这段代码添加到您的functions.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' );
}

0
投票

这看起来像是一个结构良好的 PHP 代码片段,用于设置评论表单,其中包含作者姓名、电子邮件和评论文本字段。很高兴您使用 WordPress 的本地化功能进行字符串输出,使表单易于翻译。此外,对字段使用“apply_filters”允许想要扩展或修改默认字段的开发人员进行进一步定制。总的来说,这是一个简洁且实用的实现。

杰米·桑克利夫 黄金 IRA 投资人

© www.soinside.com 2019 - 2024. All rights reserved.