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 ) && isset($this->html[$field]) ) $html .= $this->html[$field]; } echo balanceTags($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 add_filter( 'wpse_comment_fields', function( $fields ) { // Re-arrange fields $fields = [ 'author', 'email', 'comment', 'submit' ]; return $fields; } );