How can I insert the content of the variable $SALT in a specific point (line or string) of a file like wp-contet.php from wordpress using Bash script?
SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
How can I insert the content of the variable $SALT in a specific point (line or string) of a file like wp-contet.php from wordpress using Bash script?
SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
You must be logged in to post a comment.
I’m not an expert at parsing text files in bash but you should delete the lines that define the things you’re downloading from the wordpress salt and then insert the variable at the end… something like:
OK, now it’s fixed… it should look for where the salt is supposed to go and it will replace it with the info retrieved from https://api.wordpress.org/secret-key/1.1/salt/
This version defines new keys if none exist, and also replaces existing keys:
If you have
csplit
available, you can split the original wp-config.php file either side of the salt definitions, download new salts, then cat back together. This keeps the PHPdefine()
statements at the same location in wp-config.php instead of than moving them to a different location within the file:How about using sed?
I think I got this one! its a bash script using only commands normally available at the command prompt and it does -everything- (assuming httpd is your web user) except create the databases. here you go.
I built a simple CLI for just that. Try it out. It’s called
[WP-Salts-Update-CLI][1]
.WP-Salts-Update-CLI
WPSUCLI
downloads new salts from the WP API and replaces them with the ones in your wp-config.php file for every site on your server.â¡ï¸ Installation
Open command line terminal (I prefer iTerm2) and run the following command.
bash
sudo wget -qO wpsucli https://git.io/vykgu && sudo chmod +x ./wpsucli && sudo install ./wpsucli /usr/local/bin/wpsucli
This command will perform the following actions:
WPSUCLI
and rename it towpsucli
wpsucli
executablewpsucli
inside /usr/local/bin/ folder.ð Usage
Just run
wpsucli
and it will update the salts for everywp-config.php
file on your server or PC.This is the bash script that I came up with that works on my Ubuntu server. I modified the examples from above.
Its a bit of brute force in that it will only replace the 8 keys that currently are required and expects the server to return exactly the same length key every time. The script works well for my use case so I thought I would share it.
I tried the accepted solution:
However it does not work perfectly as for some reason it induces the SALTS to “move down” 1 line in the wp-config.php file each time it is used… it is not ideal if you are going to change SALTS automatically like every week, months with cron for example…
A better solution for me was to create a little function that I call in my script.
This function creates a file with the SALTS (deletes it at the end), deletes every lines containing one of the SALTS then just inserts the SALTS contained in the file in place of the initial SALTS.
This works perfectly.
The function is to be called in the script like this:
Where $SITE_PATH=”/var/www/html/YOUR_WEBSITE” or whatever path works for you.
I was challenged with the same issue. Here is the script I wrote to replace the salts and keys from ones downloaded from WordPress. You can use it at any time to replace them if/when needed. I run it as sudo, and the script tests for that. If you use an account that can download to the directory and make updates to the wp-config.php file, then you can delete that part of the script.
Many of the answers rely on the phrase
'put your unique phrase here'
being present in the file, so they do not work when you want to change salts after the first time. There are also some that remove the old definitions and append the new ones at the end. While that does work, it’s nice to keep the definitions where you would expect them, right after the comment documenting them. My solution addresses those issues.I made a few attempts with sed, perl and regex, but there are special characters in the salts and the rest of the config file that tend to mess things up. I ended up using grep to search the document for the unique comment structure that opens and closes the salt definition block, which has the following format:
Note that if that comment structure is removed or altered, this will no longer work. Here’s the script:
Strings containing single asterisks, such as
"*/"
and"/**#@-*/"
want to expand to directory lists, so that is why those asterisks are escaped.Here’s a pure bash approach. This does not depend on wordpress.org.
I converted the original wp_generate_password() function used by WordPress to generate salt.
You can then just run
SALT="$(wp_generate_password 64 1 1)"
.Update
I just published a standalone script to generate WP salt values. You can generate the salt values by running
./wp-generate-salt.sh
.If the wordpress.org API generated SALT values are not necessary for your use case, you can use the
pwgen
to generate keys on the server and insert those into wp-config.php.You may need to fix the ownership of the file after using sudo. You can use a command similar to this for changing the ownership.