I want to add a class to the form, not form items. I've looked on http://codex.wordpress.org/Function_Reference/comment_form but there is no mention of adding a class to the form.
UPDATE:
Wordpress finally supports the possibility to add classes to the comments form. See Nabil Kadimi's answer to get an example.
My outdated answer:
Because Wordpress still hasn't supported this option, I've worked out the following workaround:
<?php
ob_start();
comment_form();
echo str_replace('class="comment-form"','class="comment-form your-custom-class"',ob_get_clean());
?>
Now the standard class comment-form
will be replaced by itself plus the custom class.
In the documentation for the comment_form()
function:
WordPress 4.4.0 Introduced the 'class_form' [...] arguments.
So you would do:
// Output the comment form with a custom class:
comment_form ( array( 'class_form' => 'my_custom_class' ) );
I prefer using hooks:
/**
* Callback function for the `comment_form_defaults` filter hook
*
* @param Array $defaults Defaults.
* @return Array Defaults modified.
*/
function se_8476425_modify_comment_form_defaults( $defaults ) {
$defaults[ 'class_form' ] = 'class1 class2 class3';
return $defaults;
};
add_filter( 'comment_form_defaults', 'se_8476425_modify_comment_form_defaults' );
This solution is more generic as you can use it to modify the default function behavior and themes you don't "own".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With