How to add text to comment form #content textarea?

I have a plugin that does validation on user submitted comments. When validation fails, I want to redirect them back to the comment form, and have their comment still appear in the comment box, so they don't have to type it again.

How can I add custom content to the comment form #content textarea?

Topic esc-textarea custom-content comments content Wordpress

Category Web


You can filter 'comment_form_defaults' to change the textarea. You get an array with the default fields as argument:

add_filter( 'comment_form_defaults', 'wpse_67503_textarea_insert' );

function wpse_67503_textarea_insert( $fields )
{
    if ( /* your condition */ )
    {
        $fields['comment_field'] = str_replace(
            '</textarea>',
            'EXTRA CONTENT</textarea>',
            $fields['comment_field']
        );
    }

    return $fields;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.