Make WordPress Core

Opened 13 years ago

Last modified 5 years ago

#20302 new enhancement

Allow comment_form() to add attributes to <form> tag

Reported by: psbook's profile psbook Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3.1
Component: Comments Keywords: has-patch has-unit-tests needs-refresh
Focuses: Cc:

Description

The standard method to allow Google Analytics to track comment submissions as a 'goal' is to add a piece of JavaScript code wrapped in an 'onsumbit' attribute:
http://www.optimisationbeacon.com/analytics/track-blog-comments-in-google-analytics/
http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html

We want something like the following:

<form action="http://www.example.com/wp-comments-post.php" method="post" id="commentform" onsubmit="_gaq.push(['_trackEvent', 'Comments', 'Submit', 'POST TITLE']);">

However the current arguments submitted to comment_form() do not provide for adding attributes to <form>
http://codex.wordpress.org/Function_Reference/comment_form

I was forced to perform a workaround in which the onsubmit code was inserted by JavaScript on the fly at runtime.

So this is request to alter comment_form() such that arguments accepted provide for 'onsubmit' or other custom attributes to be appended on the <form> tag.

Attachments (5)

20302-1.diff (2.3 KB) - added by jkudish 13 years ago.
20302-2.diff (2.3 KB) - added by jkudish 13 years ago.
initial path had syntax errors, now fixed
20302.patch (4.6 KB) - added by stevegrunwell 9 years ago.
Refresh for WordPress 4.4 with addition of unit tests
20302.diff (4.6 KB) - added by stevegrunwell 9 years ago.
.diff version of the .patch file just submitted.
20302.2.diff (5.4 KB) - added by stevegrunwell 9 years ago.
Rename "attributes" to "attributes_form" (matching the existing pattern) and add necessary DocBlock for this new argument. Tests have also been refactored to remove closures.

Download all attachments as: .zip

Change History (17)

#1 @psbook
13 years ago

  • Version set to 3.3.1

#2 @jkudish
13 years ago

  • Cc joachim.kudish@… added
  • Keywords has-patch needs-testing added
  • Version changed from 3.3.1 to 3.4

The proposed/attached patch adds a 'form_attributes' argument to the $args for comment_form, it also adds 2 new filters, one for the attributes (which are passed as a key/value array) and one for the entire form element as it gets output.

@jkudish
13 years ago

@jkudish
13 years ago

initial path had syntax errors, now fixed

#3 @SergeyBiryukov
13 years ago

  • Version changed from 3.4 to 3.3.1

Version number indicates when the bug was initially introduced/reported.

Version 0, edited 13 years ago by SergeyBiryukov (next)

#4 @chriscct7
10 years ago

  • Keywords needs-refresh added; has-patch needs-testing removed

Needs a refresh because of the HTML class added in #23851

#5 @chriscct7
9 years ago

  • Keywords needs-unit-tests added

#6 follow-up: @swissspidy
9 years ago

Why is onsubmit="" needed for this? Can't that be done in a separate JS file or inline jS?

#7 in reply to: ↑ 6 @peterwilsoncc
9 years ago

Replying to swissspidy:

Why is onsubmit="" needed for this? Can't that be done in a separate JS file or inline jS?

For the use case described, yes using seperate JS would be optimal.

For other attributes, class, aria, etc this would be handy. FWIW, the filtering techniques of allowing all attributes to be filtered used for menu items appeals to me.

@stevegrunwell
9 years ago

Refresh for WordPress 4.4 with addition of unit tests

@stevegrunwell
9 years ago

.diff version of the .patch file just submitted.

#8 @stevegrunwell
9 years ago

  • Keywords has-patch added; needs-refresh needs-unit-tests removed

Just submitted a refresh for WordPress 4.4 and up. Rather than introducing a new filter (as was done earlier), I added support for the "attributes" key within the set of defaults passed to the comment_form_defaults filter. Original attributes (action, method, id, and class) are left unfiltered, so this new option is really for additional attributes that should be added to the <form> element.

#9 @swissspidy
9 years ago

I like the approach of the new patch. Some notes:

  • The closure inside test_extra_comment_form_attributes needs to go into a new method. Closures aren't supported in PHP 5.2.
  • The new attributes argument needs to be documented in the DocBlock for comment_form()
  • attributes sounds misleading. What are these attributes for? Since there's class_form and id_form already, attributes_form is probably better

@stevegrunwell
9 years ago

Rename "attributes" to "attributes_form" (matching the existing pattern) and add necessary DocBlock for this new argument. Tests have also been refactored to remove closures.

#10 @stevegrunwell
9 years ago

  • Keywords has-unit-tests added

I agree on all counts, and TIL that even the test suite needs to support 5.2.

Thanks for the feedback!

This ticket was mentioned in Slack in #core-comments by rachelbaker. View the logs.


8 years ago

#12 @rachelbaker
8 years ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to Future Release

The comment form action attribute is already filterable via comment_form_defaults(). See #26841.

During our Comments bug scrub @boonebgorges suggested creating a new filter, to prevent filter stacking and letting the comment_form_defaults filter get *even more* out of hand. His example:

$addl_attributes = apply_filters( 'comment_form_additional_attributes', array() );

if ( $addl_attributes ) { 
    // do stuff.
}
Note: See TracTickets for help on using tickets.