Improved customizability for the Submit button in comment_form()
.
The new 'submit_button' and 'submit_field' parameters for `comment_form()` allow developers to modify the markup of the submit button and its wrapper. These params are accompanied by targeted 'comment_form_submit_button' and 'comment_form_submit_field' filters on the concatenated markup. Props coffee2code, morpheu5, DrewAPicture, boonebgorges. Fixes #15015. git-svn-id: https://develop.svn.wordpress.org/trunk@31699 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
179a1b7a4c
commit
8072fe7db6
@ -2153,6 +2153,7 @@ function wp_list_comments( $args = array(), $comments = null ) {
|
||||
* in the array of fields.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 4.2.0 Introduced 'submit_button' and 'submit_fields' arguments.
|
||||
*
|
||||
* @param array $args {
|
||||
* Optional. Default arguments and form fields to override.
|
||||
@ -2180,6 +2181,11 @@ function wp_list_comments( $args = array(), $comments = null ) {
|
||||
* where %s is the author of the comment being replied to.
|
||||
* @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
|
||||
* @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
|
||||
* @type string $submit_button HTML format for the Submit button.
|
||||
* Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
|
||||
* @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden
|
||||
* fields. Default: '<p class="form-submit">%1$s %2$s</a>', where %1$s is the
|
||||
* submit button markup and %2$s is the comment hidden fields.
|
||||
* @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
|
||||
* }
|
||||
* @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
|
||||
@ -2236,6 +2242,8 @@ function comment_form( $args = array(), $post_id = null ) {
|
||||
'title_reply_to' => __( 'Leave a Reply to %s' ),
|
||||
'cancel_reply_link' => __( 'Cancel reply' ),
|
||||
'label_submit' => __( 'Post Comment' ),
|
||||
'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
|
||||
'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
|
||||
'format' => 'xhtml',
|
||||
);
|
||||
|
||||
@ -2350,11 +2358,45 @@ function comment_form( $args = array(), $post_id = null ) {
|
||||
echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );
|
||||
?>
|
||||
<?php echo $args['comment_notes_after']; ?>
|
||||
<p class="form-submit">
|
||||
<input name="<?php echo esc_attr( $args['name_submit'] ); ?>" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" class="<?php echo esc_attr( $args['class_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
|
||||
<?php comment_id_fields( $post_id ); ?>
|
||||
</p>
|
||||
|
||||
<?php
|
||||
$submit_button = sprintf(
|
||||
$args['submit_button'],
|
||||
esc_attr( $args['name_submit'] ),
|
||||
esc_attr( $args['id_submit'] ),
|
||||
esc_attr( $args['class_submit'] ),
|
||||
esc_attr( $args['label_submit'] )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filter the submit button for the comment form to display.
|
||||
*
|
||||
* @since 4.2.0
|
||||
*
|
||||
* @param string $submit_button HTML markup for the submit button.
|
||||
* @param array $args Arguments passed to `comment_form()`.
|
||||
*/
|
||||
$submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
|
||||
|
||||
$submit_field = sprintf(
|
||||
$args['submit_field'],
|
||||
$submit_button,
|
||||
get_comment_id_fields( $post_id )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filter the submit field for the comment form to display.
|
||||
*
|
||||
* The submit field includes the submit button, hidden fields for the
|
||||
* comment form, and any wrapper markup.
|
||||
*
|
||||
* @since 4.2.0
|
||||
*
|
||||
* @param string $submit_field HTML markup for the submit field.
|
||||
* @param array $args Arguments passed to `comment_form()`.
|
||||
*/
|
||||
echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
|
||||
|
||||
/**
|
||||
* Fires at the bottom of the comment form, inside the closing </form> tag.
|
||||
*
|
||||
|
55
tests/phpunit/tests/comment/commentForm.php
Normal file
55
tests/phpunit/tests/comment/commentForm.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group comment
|
||||
*/
|
||||
class Tests_Comment_CommentForm extends WP_UnitTestCase {
|
||||
public function test_default_markup_for_submit_button_and_wrapper() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$args = array(
|
||||
'name_submit' => 'foo-name',
|
||||
'id_submit' => 'foo-id',
|
||||
'class_submit' => 'foo-class',
|
||||
'label_submit' => 'foo-label',
|
||||
);
|
||||
$form = get_echo( 'comment_form', array( $args, $p ) );
|
||||
|
||||
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
|
||||
$hidden = get_comment_id_fields( $p );
|
||||
$this->assertRegExp( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
|
||||
}
|
||||
|
||||
public function test_custom_submit_button() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$args = array(
|
||||
'name_submit' => 'foo-name',
|
||||
'id_submit' => 'foo-id',
|
||||
'class_submit' => 'foo-class',
|
||||
'label_submit' => 'foo-label',
|
||||
'submit_button' => '<input name="custom-%1$s" type="submit" id="custom-%2$s" class="custom-%3$s" value="custom-%4$s" />'
|
||||
);
|
||||
$form = get_echo( 'comment_form', array( $args, $p ) );
|
||||
|
||||
$button = '<input name="custom-foo-name" type="submit" id="custom-foo-id" class="custom-foo-class" value="custom-foo-label" />';
|
||||
$this->assertContains( $button, $form );
|
||||
}
|
||||
|
||||
public function test_custom_submit_field() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$args = array(
|
||||
'name_submit' => 'foo-name',
|
||||
'id_submit' => 'foo-id',
|
||||
'class_submit' => 'foo-class',
|
||||
'label_submit' => 'foo-label',
|
||||
'submit_field' => '<p class="my-custom-submit-field">%1$s %2$s</p>'
|
||||
);
|
||||
$form = get_echo( 'comment_form', array( $args, $p ) );
|
||||
|
||||
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
|
||||
$hidden = get_comment_id_fields( $p );
|
||||
$this->assertRegExp( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user