演示-设置演示如何控制注释字段的显示及其顺序:
让我们创建一个自定义类:
$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_fields
在comment_form_fields
筛选并确保它们位于默认字段之前。这将仅在注销情况下显示。然后对于注销的情况comment_form_logged_in_after
挂钩可用。