Skip to content

Instantly share code, notes, and snippets.

@f1code
Created November 23, 2015 13:58
Show Gist options
  • Select an option

  • Save f1code/9557362e96e7cf96001f to your computer and use it in GitHub Desktop.

Select an option

Save f1code/9557362e96e7cf96001f to your computer and use it in GitHub Desktop.
Script for generating WP secret keys (like https://api.wordpress.org/secret-key/1.1/salt/, but suitable to use in .env file)
<?php
function generate_salt($length) {
$seed = 'abcdefghijklmnopqrstuvwxyz'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'0123456789!@#$%^&*()"'; // and any other characters
$rand = substr(str_shuffle(str_repeat($seed, $length)), 0, $length);
return $rand;
}
function escape_value($val) {
return '"' . preg_replace('/"|\\|\\$/', '\\\\$0', $val) . '"';
}
function generate_all() {
$keys = ['AUTH_KEY',
'SECURE_AUTH_KEY',
'LOGGED_IN_KEY',
'NONCE_KEY',
'AUTH_SALT',
'SECURE_AUTH_SALT',
'LOGGED_IN_SALT',
'NONCE_SALT'];
$salts = [];
foreach($keys as $k) {
$salts[$k] = generate_salt(64);
}
return $salts;
}
function print_all($salts) {
foreach($salts as $name => $val) {
echo $name . '=' . escape_value($val) . "\n";
}
}
print_all(generate_all());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment