备注表单自定义域顺序

时间:2016-07-13 作者:anton

我正在尝试向评论表单添加5个自定义字段。只需使用2个挂钩在默认字段之后显示自定义字段-“comment\\u form\\u logged\\u in\\u after”和“comment\\u form\\u after\\u fields”如下所示:

function kvkoolitus_registration_additional_fields () {

  echo \'<p class="comment-form-code">\'.
  \'<label for="code">\' . __( \'Personal code\', \'kvkoolitus\' ) . \' <span class="required">*</span> </label>\'.
  \'<input id="code" name="code" type="text" size="30"  tabindex="5" /></p>\';

  echo \'<p class="comment-form-phone">\'.
  \'<label for="phone">\' . __( \'Phone\',\'kvkoolitus\' ) . \' <span class="required">*</span> </label>\'. 
  \'<input id="phone" name="phone" type="text" size="30"  tabindex="5" /></p>\';

  echo \'<p class="comment-form-address">\'.
  \'<label for="address">\' . __( \'Address\',\'kvkoolitus\' ) . \'</label>\'.
  \'<input id="address" name="address" type="text" size="30"  tabindex="5" /></p>\';

  echo \'<p class="comment-form-company">\'.
  \'<label for="company">\' . __( \'Company\',\'kvkoolitus\' ) . \'</label>\'.
  \'<input id="company" name="company" type="text" size="30"  tabindex="5" /></p>\';

  echo \'<p class="comment-form-profession">\'.
  \'<label for="profession">\' . __( \'Profession\',\'kvkoolitus\' ) . \'</label>\'.
  \'<input id="profession" name="profession" type="text" size="30"  tabindex="5" /></p>\'; 
}

add_action( \'comment_form_logged_in_after\', \'kvkoolitus_registration_additional_fields\' );
add_action( \'comment_form_after_fields\', \'kvkoolitus_registration_additional_fields\' );
当有人登录时,首先是必填字段(姓名、电子邮件)、其他字段,然后是评论字段,就像我需要的一样。但若用户未登录,注释字段将显示在第一个位置。为什么会发生这种情况,如何解决?wrong order

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

演示-设置演示如何控制注释字段的显示及其顺序:

让我们创建一个自定义类:

$mycf = new MyCommentFields;
然后定义额外字段:

$myfields = [];

$myfields[\'myaddress\'] = sprintf(
    \'<p class="comment-form-myaddress">
         <label for="myaddress">%s <span class="required">*</span></label>
         <input id="myaddress" name="myaddress" type="text" size="30" tabindex="5" />
     </p>\', 
    esc_html__( \'Address\', \'kvkoolitus\' )
); 

$myfields[\'myphone\'] = sprintf(
    \'<p class="comment-form-myphone">
         <label for="myphone">%s <span class="required">*</span></label>
         <input id="myphone" name="myphone" type="text" size="30" tabindex="5" />
     </p>\', 
    esc_html__( \'Phone\', \'kvkoolitus\' )
);
并设置为:

$mycf->set_extra_fields_logged_in(  $myfields );
$mycf->set_extra_fields_logged_out( $myfields );
接下来,我们设置这些字段的显示顺序:

$mycf->set_fields_order_logged_in( 
    [ 
        \'myaddress\', 
        \'myphone\', 
        \'comment\', 
    ] 
);
以及:

$mycf->set_fields_order_logged_out( 
    [ 
        \'author\', 
        \'email\', 
        \'url\', 
        \'myaddress\', 
        \'myphone\', 
        \'comment\' 
    ] 
);
然后我们初始化它:

$mycf->init();
然后我们可以把它挂在comment_form_before 行动

演示-类

这是我们的演示类(PHP 5.4+):

class MyCommentFields
{
    private $extra_fields_logged_in;
    private $extra_fields_logged_out;
    private $fields_order_logged_in;
    private $fields_order_logged_out;

    public function init()
    {
        // logged-out case
        add_filter( \'comment_form_fields\',          [ $this, \'comment_form_fields\' ] );

        // logged-in case
        add_filter( \'comment_form_field_comment\',   [ $this, \'comment_form_field_comment\' ] );      
    }

    public function set_extra_fields_logged_in( array $extra_fields_logged_in )
    {
        $this->extra_fields_logged_in = $extra_fields_logged_in;
    }

    public function set_extra_fields_logged_out( array $extra_fields_logged_out )
    {
        $this->extra_fields_logged_out = $extra_fields_logged_out;
    }

    public function set_fields_order_logged_in( array $fields_order_logged_in )
    {
        $this->fields_order_logged_in = $fields_order_logged_in;
    }

    public function set_fields_order_logged_out( array $fields_order_logged_out )
    {
        $this->fields_order_logged_out = $fields_order_logged_out;
    }

    public function comment_form_field_comment( $field )
    {
        // logged-in case
        if( is_user_logged_in() && ! empty( $this->extra_fields_logged_in ) ) 
        {
            $tmp = \'\';
            foreach( $this->fields_order_logged_in as $key )
            {
                if( \'comment\' === $key  )
                    $tmp .= $field;
                elseif( isset( $this->extra_fields_logged_in[$key] ) )
                    $tmp .= $this->extra_fields_logged_in[$key];
            }
            if( $tmp )
                $field = $tmp;
        }
        return $field;
    }

    public function comment_form_fields( $fields )
    {
        // logged-out case
        if( ! is_user_logged_in() && ! empty( $this->extra_fields_logged_out ) ) 
        {
            $tmp = [];
            foreach( $this->fields_order_logged_out as $key )
            {
                if( isset( $fields[$key] ) )
                    $tmp[$key] = $fields[$key];             
                elseif( isset( $this->extra_fields_logged_out[$key] ) )
                    $tmp[$key] = $this->extra_fields_logged_out[$key];
            }   
            if( ! empty( $tmp ) ) 
                $fields = $tmp;
        }       
        return $fields;
    }

} // end class
希望您可以根据需要调整演示。

前面的答案:

这里有一个技巧,可以预先添加额外的字段,它应该适用于登录和注销情况:

add_filter( \'comment_form_fields\', function( $fields )
{
      echo \'<p class="comment-form-code">\'.
           \'<label for="code">\' . __( \'Personal code\', \'kvkoolitus\' ) . \' <span class="required">*</span> </label>\'.
           \'<input id="code" name="code" type="text" size="30"  tabindex="5" /></p>\';

      echo \'<p class="comment-form-phone">\'.
           \'<label for="phone">\' . __( \'Phone\',\'kvkoolitus\' ) . \' <span class="required">*</span> </label>\'. 
           \'<input id="phone" name="phone" type="text" size="30"  tabindex="5" /></p>\';

      echo \'<p class="comment-form-address">\'.
           \'<label for="address">\' . __( \'Address\',\'kvkoolitus\' ) . \'</label>\'.
           \'<input id="address" name="address" type="text" size="30"  tabindex="5" /></p>\';

      echo \'<p class="comment-form-company">\'.
           \'<label for="company">\' . __( \'Company\',\'kvkoolitus\' ) . \'</label>\'.
           \'<input id="company" name="company" type="text" size="30"  tabindex="5" /></p>\';

      echo \'<p class="comment-form-profession">\'.
           \'<label for="profession">\' . __( \'Profession\',\'kvkoolitus\' ) . \'</label>\'.
           \'<input id="profession" name="profession" type="text" size="30"  tabindex="5" /></p>\'; 

    return $fields;
} );
请注意,动态comment_form_field_{$name} 对于这些字段,注销情况的过滤器将不可用。

否则,可以将新字段注入$comments_fieldscomment_form_fields 筛选并确保它们位于默认字段之前。这将仅在注销情况下显示。然后对于注销的情况comment_form_logged_in_after 挂钩可用。

相关推荐

如何根据评论元字段对“EDIT-Comments.php”表中的评论进行排序?

目标在;编辑注释。php“;单击自定义列的标题后,基于注释元字段的表。上下文为了简洁起见,我们假设在发布评论时,一个名为;“红心”;指定给它,以便所有注释都具有从0到10的正整数字段。的bold 下面提到的步骤,前三个步骤是针对上下文和预期工作给出的,问题是在第四个步骤上。1. Creating the columnadd_filter( \'manage_edit-comments_columns\', \'hearts_add_comments_column\' ); function hea