I have a page with multiple wp_editors. I need to make 2 of them required. I tried the following, but it didn't work. I did put it right after the function that loads the page, but not sure if it should be somewhere else. Any help is appreciated.

Code to load the wp_editor

$content = "";
$editorid = "statement";
$settings = array(  
    'textarea_name' => 'statement',
    'editor_height' => 100,
    'quicktags' => false,
    'media_buttons' => false,                       
    'teeny' => true,
    'tinymce'=> array(
        'theme_advanced_disable' => 'fullscreen',
        'width' => 500,
wp_editor( $content, $editorid, $settings );

Code to make the editor required

add_filter( 'statement', 'add_required_attribute_to_wp_editor', 10, 1 );
function add_required_attribute_to_wp_editor( $editor ) {
    $editor = str_replace( '<textarea', '<textarea required="required"', $editor );
    return $editor;

I can't find any documentation that will allow adding any tag attributes to the wp_editor except for the id. You've got two options that I can think of. The first is to include a jQuery dependent (or not) javascript file which will add the required attribute to the editor textarea after the DOM is fully rendered.

The other option is to include a validation function either server side or browser side to make sure the data is present. I suggest this approach because the required attribute is not universally implemented across browsers.

Your code won't work because there's no apply_filter function that fires on $statement.

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.