0

I'm trying to add a PureCSS class to the <form> tag in the default comments template, but the class isn't being added. Here's what I've done so far:

function pwp_comments_form_pure($output) {
    $output = preg_replace('/class="comment-form"/', 'class="comment-form pure-form"', $output);
    return $output;
}
add_filter('comments_template', 'pwp_comments_form_pure');

I know the preg_replace approach works, because I did the same for the default search form and it worked without any problems:

function pwp_search_form_pure($output) {
    $output = preg_replace('/class="searchform"/', 'class="searchform pure-form"', $output);
    return $output;
}
add_filter('get_search_form', 'pwp_search_form_pure');

I've triple-checked the class names and hyphenation, and they all match up.

I've also tried adding a priority parameter of 1 and 100 to the comments_template filter, but it didn't make a difference.

Is there an override somewhere in the WordPress defaults that I'm not aware of?

3

Any comments_template filter should return an absolute filepath to the comments template - use comment_form_defaults and set the class_form argument:

add_filter( 'comment_form_defaults', function ( $args ) {
    $args['class_form'] = 'my form classes';
    return $args;
});
  • That's exactly what I needed, thank you. I hadn't fully appreciated that comments_template was actually for templates! – tristanojbacon Oct 4 '16 at 15:22
1

The filter you are using is about the template path, it doesn't do anything with the form output. The class you want to add doesn't need a filter. You can set it when you set the arguments for your call to comment_form in your template.

$comment_args        =  array (
  'id_form'         =>  'comment-form',
  'class_form'      =>  'comment-form pure-form',
  ....
  );
$comment_form ($comment_args);
  • 1
    An equally good solution, and the only reason I didn't accept it was because I need a solution that sits in my theme's functions.php file, rather than in the template. – tristanojbacon Oct 4 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.