WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 6 weeks ago

#15015 new enhancement

Customisable submit button for comment form

Reported by: morpheu5 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0.1
Component: Comments Keywords: dev-feedback has-patch
Focuses: Cc:

Description

Note: I'm setting this as a blocker because it is a blocker to me. Set it whatever you feel appropriate.

I badly needed to customise the submit button because I wanted to add a tabindex.

I could reimplement the whole form with my markup, but as I already worked my way through all the other fields, I did want to continue this way.

Sadly I discovered it's not possible. So, after discussing this in IRC, I decided to hack the core and propose the attached patch.

Basically now you can create a filter and output your markup, like this:

function awesome_comment_form_submit_button($button) {
	$button =
		'<input name="submit" type="submit" class="form-submit" tabindex="5" id="[args:id_submit]" value="[args:label_submit]" />' .
		get_comment_id_fields();
	return $button;
}
add_filter('comment_form_submit_button', 'awesome_comment_form_submit_button');

and filter magic happens. Please notice that

  1. you have to include [args:id_submit] and [args:label_submit] if you want the comment_form() parameters to work.
  2. you have to use the get_comment_id_fields() function.

It may be better but it works for me. If anybody feels like making it better, be my guest.

Attachments (2)

comment-template.patch (1.5 KB) - added by morpheu5 4 years ago.
Proposed patch
15015.diff (1.3 KB) - added by coffee2code 2 years ago.

Download all attachments as: .zip

Change History (24)

morpheu54 years ago

Proposed patch

comment:1 sivel4 years ago

  • Severity changed from blocker to trivial

comment:2 morpheu54 years ago

  • Cc andrea.franceschini@… added

comment:3 follow-up: demetris4 years ago

  • Cc dkikizas@… added
  • Severity changed from trivial to normal
function my_comment_form_defaults($i)
{
    $i['id_submit'] = 'submit" tabindex="5';
        
    return $i;
}
add_filter('comment_form_defaults', 'my_comment_form_defaults');

Would a workaround like that work for your case?

It would be good if the comment form was fully customizable in everything (that is, without any hard-coded HTML in it at all), and I’ve wished that myself a couple of times, but I wouldn’t say this is a blocker. :-)

comment:4 in reply to: ↑ 3 morpheu54 years ago

Replying to demetris:

function my_comment_form_defaults($i)
{
    $i['id_submit'] = 'submit" tabindex="5';
        
    return $i;
}
add_filter('comment_form_defaults', 'my_comment_form_defaults');

Would a workaround like that work for your case?

That'd certainly be. However, I don't need to remind you how bad this practice would be. I'll never surrender myself to such immoral code :)

It would be good if the comment form was fully customizable in everything (that is, without any hard-coded HTML in it at all), and I’ve wished that myself a couple of times, but I wouldn’t say this is a blocker. :-)

Agreed :)

comment:5 morpheu54 years ago

Hi there. Any plan to work on this? I need to know if I can rely on something like this or if I have to go for dirty workarounds.

The point is that I need to almost completely redesign the markup of the form. In fact I'd need to get rid of the <p> that surrounds the submit button and replace it with a <div>. This is because I have a theme developed for Drupal and I need to keep that in sync with the one for Wordpress, and there's no way Drupal's theme is going to be changed.

comment:6 morpheu54 years ago

FTR, I just came up with an almost-acceptable workaround. I basically copied comment_form() into my theme's functions.php, renamed it awesome_comment_form() and used it instead of comment_form(). This way I could keep the original comment_form() function and apply the modifications to my own.

However I still hope someone's going to decide something about this patch :)

comment:7 morpheu53 years ago

Anyone?

comment:8 dd323 years ago

  • Keywords needs-patch dev-feedback added; submit button markup custom removed
  • Milestone changed from Awaiting Review to Future Release

Future Release at this point, The attached patch uses a placeholder syntax which we don't use for a start.

The place holders we generally use are sprintf() style placeholders.

comment:9 morpheu53 years ago

I'm not sure what you do mean by "placeholder syntax". If I had seen sprintf()s in the original code, I'd have used them.

comment:10 dd323 years ago

by placeholder syntax, i meant the usage of "[args:id]" as a placeholder in the original string.

Generally, we tend to do it via "some String %1$s goes %2$s <- here"

comment:11 anointed2 years ago

Would love to see changes made in the next vs. where we can apply classes to the submit button. I ran into this problem today and really could have saved myself a ton of time if I were able to add classes as well as the id.

I wanted classes so that I could do something like class="btn primary success" using my boilerplate. This obviously doesn't work using id's.

coffee2code2 years ago

comment:13 follow-up: coffee2code2 years ago

  • Keywords has-patch added; needs-patch removed

As @dd32 stated, the original patch comment-template.patch does not conform to the WP way of doing things.

I've attached 15015.diff as an alternative solution. It treats the submit button in the same way the comment input field (comment_field) is handled:

  • A 'submit_button' entry is added to the comment form's $defaults array and contains the base markup for the button. This allows the entire 'input' tag to be filtered via the 'comment_form_defaults' filter.
  • Additionally, also in the same vein as 'comment_field', the finalized (sprintf()'d) version of the submit button is passed through the new 'comment_form_submit_button' filter. This allows an additional way to customize the submit button markup prior to display.

This approach is more consistent with what the function already does/allows and permits arbitrary customization of the submit button markup.

@morpheu5 could get the 'tabindex' attribute he wants, and @anoited could add 'class' (which are also the requests in the related tickets I pointed out) -- both without needing to add specific support for those (or other) attributes.

comment:14 carstenbach22 months ago

  • Cc carstenbach added

comment:15 mercime16 months ago

  • Cc mercijavier@… added

comment:16 in reply to: ↑ 13 Greenweb10 months ago

Any movement on this ?
Replying to coffee2code:

As @dd32 stated, the original patch comment-template.patch does not conform to the WP way of doing things.

I've attached 15015.diff as an alternative solution. It treats the submit button in the same way the comment input field (comment_field) is handled:

  • A 'submit_button' entry is added to the comment form's $defaults array and contains the base markup for the button. This allows the entire 'input' tag to be filtered via the 'comment_form_defaults' filter.
  • Additionally, also in the same vein as 'comment_field', the finalized (sprintf()'d) version of the submit button is passed through the new 'comment_form_submit_button' filter. This allows an additional way to customize the submit button markup prior to display.

This approach is more consistent with what the function already does/allows and permits arbitrary customization of the submit button markup.

@morpheu5 could get the 'tabindex' attribute he wants, and @anoited could add 'class' (which are also the requests in the related tickets I pointed out) -- both without needing to add specific support for those (or other) attributes.

comment:17 SergeyBiryukov10 months ago

#20446 was marked as a duplicate.

comment:18 jml6m9 months ago

  • Cc jml6m added

Would love to have this functionality added, I'm making all of my Submit button changes client side with jQuery at the moment.

Last edited 9 months ago by jml6m (previous) (diff)

comment:19 follow-up: SergeyBiryukov5 months ago

#26142 was marked as a duplicate.

comment:20 in reply to: ↑ 19 dwainm5 months ago

Replying to SergeyBiryukov:

#26142 was marked as a duplicate.

Sorry about the duplicate. Do you know if this is being considered?

comment:21 nofearinc4 months ago

Related to new hooks for #26395, can we add this one as I have also had issues with that and I have to solve them with JS for some of them?

The patch from coffee2code looks good to me, and uses sprintf and values that could be overridden in the defaults.

Last edited 6 weeks ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.