重新排列注释字段
注释文本区域已
moved 到WordPress 4.4的顶部:
如果我们更喜欢旧的设置,它在底部呢?
重新安排author
, url
和email
注释字段
我们可以修改作者、url和电子邮件字段的显示顺序,包括:
/**
* Re-arranging the author, url and email comment fields
*/
add_filter( \'comment_form_defaults\', function( $defaults )
{
$_defaults = [];
// Re-arrange the fields to your needs:
$fields = [ \'author\', \'url\', \'email\' ];
foreach( $fields as $field )
$_defaults[\'fields\'][$field] = $defaults[\'fields\'][$field];
return $_defaults;
} );
但我们必须找到另一种方法来重新排序评论文本区域和提交按钮。
自定义解决方法要重新排列所有五个注释字段,我们可以使用自定义wpse_comment_fields
过滤器:
add_filter( \'wpse_comment_fields\', function( $fields )
{
return $fields;
} );
在
functions.php
文件或插件中。
示例:Author
顶部字段
让我们将作者字段移到顶部:
/**
* Display the \'author\' comment field at top:
*/
add_filter( \'wpse_comment_fields\', function( $fields )
{
// Re-arrange fields
$fields = [ \'author\', \'comment\', \'url\', \'email\', \'submit\' ];
return $fields;
} );
然后,注释表单将显示为:
示例:Comment
字段并移除Url
字段
此处,我们在底部显示注释字段,并删除url字段:
/**
* Display the \'comment\' field at the bottom and remove the \'url\' field:
*/
add_filter( \'wpse_comment_fields\', function( $fields )
{
// Re-arrange fields
$fields = [ \'email\', \'author\', \'comment\', \'submit\' ];
return $fields;
} );
然后,评论表单将显示:
演示插件wpse_comment_fields
过滤器(PHP 5.4以上):
<?php
/**
* Plugin Name: Rearrange Comment Fields
* Plugin URI: http://wordpress.stackexchange.com/a/207449/26350
* Author: Birgir Erlendsson (birgire)
* Description: Support for rearranging the comment fields: \'comment\', \'auhtor\', \'url\', \'email\' and \'submit\' through the \'wpse_comment_fields\' filter.
* Version: 0.0.1
*/
add_action( \'comment_form_before\', function()
{
if( class_exists( \'WPSE_Rearrange_Comment_Fields\' ) )
{
$fields = apply_filters(
\'wpse_comment_fields\',
[ \'comment\', \'author\', \'url\', \'email\', \'submit\' ]
);
$o = new WPSE_Rearrange_Comment_fields;
$o->set_fields( $fields )
->init();
}
});
class WPSE_Rearrange_Comment_Fields
{
private $html = [];
private $defaults = [];
private $fields = [];
public function set_fields( array $fields )
{
$this->fields = $fields;
return $this;
}
public function init()
{
// Default
$this->defaults = [ \'comment\', \'author\', \'url\', \'email\', \'submit\' ];
// Check for defaults
if( empty( $this->fields ) )
$this->fields = $this->defaults;
// Hooks
add_action( \'comment_form\', [$this, \'display\'], PHP_INT_MAX );
add_filter( \'comment_form_field_comment\', [$this, \'comment_form_field_comment\'], PHP_INT_MAX );
add_filter( \'comment_form_submit_field\', [$this, \'comment_form_submit_field\'], PHP_INT_MAX );
foreach( [ \'author\', \'url\', \'email\' ] as $field )
add_filter( "comment_form_field_{$field}", [$this, \'comment_form_field\'], PHP_INT_MAX );
}
public function display()
{
// Display fields in the custom order
$html = \'\';
foreach( (array) $this->fields as $field )
{
if( in_array( $field, $this->defaults ) )
$html .= $this->html[$field];
}
echo $html;
}
public function comment_form_submit_field( $submit_field )
{
$this->html[\'submit\'] = $submit_field;
return \'\';
}
public function comment_form_field_comment( $comment_field )
{
$this->html[\'comment\'] = $comment_field;
return \'\';
}
public function comment_form_field( $field )
{
$key = str_replace( \'comment_form_field_\', \'\', current_filter() );
$this->html[$key] = $field;
return \'\';
}
} // end class