Craig Ward
Craig Ward

Reputation: 2485

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.

Upvotes: 4

Views: 7688

Answers (5)

Vincent Rocher
Vincent Rocher

Reputation: 73

You can easily modify the code of the submit button of the coment form with this filter :

function custom_submit_comment_form( $submit_button ) {
  return '<input name="submit" type="submit" id="submit" class="btn btn_2" value="Laisser un commentaire" />';
}
add_filter( 'comment_form_submit_button', 'custom_submit_comment_form' );

Upvotes: 2

chrisbergr
chrisbergr

Reputation: 198

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.

Upvotes: 8

Nabil Kadimi
Nabil Kadimi

Reputation: 10394

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

Upvotes: 7

Jorge Palacio
Jorge Palacio

Reputation: 1345

Since wordpress versión 4.1 (Dec, 2014) the comment_form function allows to specify a class attribute for the submit button.

Php Code:

$comments_args = array('class_submit' => 'btn btn-default');
comment_form($comments_args);

Resultant HTML Button code:

<input name="submit" type="submit" id="submit" class="btn btn-default" value="Submit" />

For reference see the related ticket: https://core.trac.wordpress.org/ticket/20446

Upvotes: 3

Jon Kuperman
Jon Kuperman

Reputation: 47

You can just edit your single.php and wrap the :

<?php comments_template(); ?>

In a class. Something like:

<div class="myClass">
<?php comments_template(); ?>
</div>

Upvotes: -3

Related Questions