php - Make wp_editor required

admin2025-01-07  6

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 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;
}
Share Improve this question asked May 15, 2019 at 14:58 Darth Mikey DDarth Mikey D 931 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264719a989.html

最新回复(0)