I'm looking for add a password generator with 14 characters on the protected pages like in the picture. I search something like what is used when you create users but for page password.
1 Answer
It is not possible to modify the post submit meta boxes with a filter.
But you can do that with JavaScript. This script will generate 10-12 random alphanumeric string and put it in the password field (if it is empty) when you click the Password protected radio button.
$('#visibility-radio-password').click(function () {
// If there is no password
var $password = $('#post_password')
if ( !$password.val() ) {
$password.val(Math.random().toString(36).slice(2));
}
});
Here you can learn how to correctly add JS scripts to admin pages - https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
-
Thanks. I add your code on file called admin.js and put it on folder called JS . I also put this code on function.php : add_action('admin_init', 'custom_admin_js'); function custom_admin_js() { wp_register_script( 'admin-js', get_stylesheet_directory_uri() . '/js/admin.js' ); wp_enqueue_script( 'admin-js' ); } . But it does not work . any idea?– SamuelCommented Nov 29, 2017 at 12:00
-
Change the
admin_init
hook toadmin_enqueue_scripts
. And as 3rd parameter ofwp_register_script
addarray('jquery')
as this script depends on it.– Greg36Commented Nov 29, 2017 at 12:08 -
Is it possible for you to put the correct full code? Thanks a lot– SamuelCommented Nov 29, 2017 at 12:17
-
Your admin.js file should look like this: pastebin.com/31BvVSEt And in
functions.php
you should have this included: pastebin.com/8jxBs5PL– Greg36Commented Nov 29, 2017 at 12:44 -
Thanks a lot Greg36. I add your code on admin.js and functions.php but it does not work. if you have time can you try it on you wordpress installation and tell me please. Thanks again for your help!– SamuelCommented Nov 29, 2017 at 13:20