1 | initial version |
To add a maxlength
attribute to the WordPress post title, you can use the maxlength
parameter of the wp_editor()
function.
In your theme’s functions.php file, add the following code:
function custom_title_maxlength() {
add_filter( 'tiny_mce_before_init', 'custom_tiny_mce_before_init' );
}
add_action( 'admin_head-post.php', 'custom_title_maxlength' );
add_action( 'admin_head-post-new.php', 'custom_title_maxlength' );
function custom_tiny_mce_before_init( $init ) {
$init['setup'] = "function (ed) {
ed.on('init', function() {
var wpTitle = document.getElementById('title');
if(wpTitle) {
wpTitle.setAttribute('maxlength', '100');
}
});
}";
return $init;
}
This code uses the tiny_mce_before_init
filter to add a JavaScript function to the setup
parameter of the TinyMCE editor initialization. The function then adds a maxlength
attribute to the post title input element with an ID of title
.
Change the number 100 to the desired character limit.