UpdraftPlus
Back up, restore and migrate your WordPress website with UpdraftPlus
Sometimes a site owner has reasons for wishing to store his remote storage credentials (e.g. Amazon S3 keys, or FTP login credentials) on-disk, instead of in the WordPress database.
This is now possible, assuming that you are running a current version of UpdraftPlus. To do so, create an mu-plugin. To do this, create a file with any name that ends in .php inside your WordPress install’s wp-content/mu-plugins folder. This folder may not already exist; if there is no folder called mu-plugins inside your wp-content folder, then you should create one.
So, for example, your file may be called wp-content/mu-plugins/define_udp_api_keys.php
Then in this file you can add this example code. The code below is for Amazon S3, and gives an example in which you have two S3 accounts that you are sending the backup set to. Below that there is another example, for FTP. If you are not sure how to adapt it for a different storage setup, then try fiverr.com or somewhere like it – there are plenty of developers who will be able to do so in a few moments.
The below code allows you to change the S3 settings to use your access key and secret key that is stored in the array in the function get_s3_user_settings. That is the function that you need to edit and insert your keys in. You do not need to edit anywhere else. To use your own API keys just replace the “test_key” and “test_secret_key” with your access key and secret key.
If you have multiple S3 instances then just add your second set of keys to the next entry in the array and if you have more than two instances then just add a new entry in that array using the same format.
<?php
add_filter('updraftplus_get_option', 'get_stored_s3_options', 10, 3);
add_filter('updraftplus_update_option', 'update_stored_s3_options', 10, 3);
// This function inserts your preferred keys when they are needed at run-time.
function get_stored_s3_options($ret, $option, $default) {
if ('updraft_s3' != $option || empty($ret['settings']) return $ret;
$user_settings = get_s3_user_settings();
foreach ($ret['settings'] as $instance => $options) {
$settings = array_shift($user_settings);
$ret['settings'][$instance]['accesskey'] = $settings['accesskey'];
$ret['settings'][$instance]['secretkey'] = $settings['secretkey'];
}
return $ret;
}
// This function makes sure that nothing is saved in the WP database
function update_stored_s3_options($value, $option, $use_cache) {
if ('updraft_s3' != $option || empty($value['settings'])) return $value;
foreach ($value['settings'] as $instance => $options) {
$value['settings'][$instance]['accesskey'] = '';
$value['settings'][$instance]['secretkey'] = '';
}
return $value;
}
// This is the function in which you should store the access keys that you want to use.
function get_s3_user_settings() {
return array(
// Instance settings 1
0 => array(
'accesskey' => 'test_key',
'secretkey' => 'test_secret_key'
),
// instance settings 2
1 => array(
'accesskey' => 'test_key_2',
'secretkey' => 'test_secret_key_2'
)
);
}
That’s it… your API keys are now saved inside this file on disk rather than inside the Database.
Here is an example of how to do the above with FTP:
<?php
add_filter('updraftplus_get_option', 'get_stored_ftp_options', 10, 3);
add_filter('updraftplus_update_option', 'update_stored_ftp_options', 10, 3);
function get_stored_ftp_options($ret, $option, $default) {
if ('updraft_ftp' != $option || empty($ret['settings'])) return $ret;
$user_settings = get_ftp_user_settings();
foreach ($ret['settings'] as $instance => $options) {
$settings = array_shift($user_settings);
$ret['settings'][$instance]['user'] = $settings['user'];
$ret['settings'][$instance]['pass'] = $settings['pass'];
}
return $ret;
}
// This function ensures that your FTP settings are not saved in the database upon saving
function update_stored_ftp_options($value, $option, $use_cache) {
if ('updraft_ftp' != $option || empty($value['settings'])) return $value;
foreach ($value['settings'] as $instance => $options) {
$value['settings'][$instance]['user'] = '';
$value['settings'][$instance]['pass'] = '';
}
return $value;
}
// Insert your credentials in this function
function get_ftp_user_settings() {
return array(
0 => array(
'user' => 'test_login',
'pass' => 'test_password'
),
1 => array(
'user' => 'test_login_2',
'pass' => 'test_password_2'
)
);
}
Try TeamUpdraft’s full suite of WordPress plugins.
Back up, restore and migrate your WordPress website with UpdraftPlus
Speed up and optimize your WordPress website. Cache your site, clean the database and compress images
Secure your WordPress website. Comprehensive, feature rich and easy to use
Centrally manage all your WordPress websites’ plugins, updates, backups, users, pages and posts from one location