Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Oliver Williams
Oliver Williams
6,278 Points

comment_form array

I have copy and pasted some code. It works but I don't understand why it works. One of the default arguments for comment_form() is an array called $fields. I can understand how easy it is to overwrite or unset elements of this array. However, $fields does not have a comment_notes_before key so why does this code work? $fields has only email, url and author key/values.

function change_up($fields) { $fields['comment_notes_before'] = '<p class="comment-notes">All fields are mandatory.</p>'; return $fields; } add_filter('comment_form_defaults','change_up');

2 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

Codex Reference

You'll see here that the first parameter for comment_form() is an array, which you've named $fields. By default, wordpress refers to this array as $args.

If you scroll down the Codex Reference to see the default field values for $args, you'll see that 'fields' is a key as well as 'comment_notes_before' (among many others). $args['fields'] (or $fields['fields'] in your case) contains the email, url and author key/values.

So really you do have access to 'comment_notes_before' as well as the other default keys shown in the reference. Hopefully you can understand better how your pasted code works now.

Oliver Williams
Oliver Williams
6,278 Points

Thank you! Such a clear answer to something I was finding overly confusing! Is there any difference between passing an array of arguments directly into a function like comment_form() and using a filter? I don't understand why you might use one rather than the other and they both seem to do the same thing.