Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding class to the comment form in Wordpress

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.

like image 374
Craig Ward Avatar asked Dec 04 '22 18:12

Craig Ward


2 Answers

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.

like image 144
chrisbergr Avatar answered Dec 11 '22 15:12

chrisbergr


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' ) );

One a second thought

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".

like image 20
Nabil Kadimi Avatar answered Dec 11 '22 16:12

Nabil Kadimi