0

What's the best way to set the required atribute on html forms in wordpress, for instance I would like post-content to be required, so this code need to be changed:

<textarea class="wp-editor-area" style="height: 300px; margin-top: 37px;" autocomplete="off" cols="40" name="content" id="content"></textarea>

To appear like this:

<textarea required class="wp-editor-area" style="height: 300px; margin-top: 37px;" autocomplete="off" cols="40" name="content" id="content"></textarea>

How can do it by a filter or action hook? This same solution I will also use it for other fields in the publish post form.

What I want is to add the HTML5 required attribute on certain fields, once the page has been rendered.

https://www.w3schools.com/tags/att_input_required.asp https://www.w3schools.com/tags/att_textarea_required.asp

3

Don't rely entirely on JavaScript validations. Use below hook for Server side validation.

function check_if_post_content_set( $maybe_empty, $postarr ) {
// Check if post is already created. IMPORTANT
if($postarr['ID'] && (int)$postarr['ID'] > 0){
    if( !$postarr['post_content'] OR $postarr['post_content'] == '' OR $postarr['post_content'] == NULL ){
        $maybe_empty = true;
    }
}
return $maybe_empty;
}
add_filter( 'wp_insert_post_empty_content', 'check_if_post_content_set', 999999, 2 );
  • This is fine to control that the field is not empty, but that already does HTML5 if we indicate the field as required. What I want is to add an HTML5 attribute, once the page has been rendered – TorryVic Apr 21 '17 at 12:51
  • So you want to add some predefined content on new post if post_content is empty ? – JItendra Rana Apr 21 '17 at 12:57
  • No. What I want is to add the HTML5 required attribute on certain fields on the publish post form, once the page has been rendered, and before the user submit the form. – TorryVic Apr 21 '17 at 13:00
  • Just came across this plugin .Fits your requirements. – JItendra Rana Apr 21 '17 at 13:39
  • this plugin hasn’t been updated in over 2 years and dosen't make what I want. I would like adding the HTML5 required atributte and make it as simple as possible – TorryVic Apr 21 '17 at 14:58
0

Finally I have solved it as follows:

add_action( 'admin_enqueue_scripts', array($this, 'my_admin_scripts') );

function my_admin_scripts($page) {
    global $post;

    if ($page == "post-new.php" OR $page == "post.php") {
        wp_register_script( 'my-custom-admin-scripts', plugins_url('/js/my-admin-post.js',dirname(__FILE__)), array('jquery', 'jquery-ui-sortable' ) , null, true );
        wp_enqueue_script( 'my-custom-admin-scripts' );             
    }           
}

And put the required atribute by JQuery in the next js code (/js/my-admin-post.js):

// JavaScript Document
jQuery(document).ready(function($) {
    $('#title').attr('required', true);
    $('#content').attr('required', true);
    $('#_my_custom_field').attr('required', true); 
});
  • For the editor area, when in Visual mode I get in the JS console An invalid form control with name='content' is not focusable. and no HTML5 tooltip pops out as warning indicating the field is required. However if I switch to the Text tab it works with no issue. Do you find a work around to this? – elvismdev Oct 26 '17 at 19:25

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.