Good day! Is it possible in WordPress to limit the number of characters entered in the text field?
It is the input data. That is, that it was impossible to write more characters in the admin panel than specified.
Good day! Is it possible in WordPress to limit the number of characters entered in the text field?
It is the input data. That is, that it was impossible to write more characters in the admin panel than specified.
If simplified, it can be done like this:
add_filter( 'tiny_mce_before_init', 'my_content_limit' ); function my_content_limit( $initArray ) { $initArray['setup'] = <<<JS [function (editor) { editor.on('keydown', function( e ) { if (e.keyCode == 8 || e.keyCode == 46) { return; } var content = tinyMCE.activeEditor.getContent(); var max = 300; // Лимит на 300 символов var len = content.length; if (len >= max ) { alert( 'Вы ввели больше ' + max + ' символов!' ); tinyMCE.activeEditor.setContent(content.substr(0, max)); } }); }][0] JS; return $initArray; } Of course, you need to add a check on the record type, handle the insertion / deletion of text, etc. But the basic principle of this example demonstrates.
UPD: Still, it is strongly recommended to check the length of the string on the server side, as KAGG Design wrote in its answer .
Something like this should look like the code in functions.php
add_filter('the_content', 'the_content_filter'); function the_content_filter( $content ){ global $post; $max_length = 30; if ( 'promo_post' === $post->post_type ) { $content = mb_substr( $content, 0, $max_length ); } return $content; } You need to know how your post type is registered - promo_post or otherwise.
Source: https://ru.stackoverflow.com/questions/785524/
All Articles