I have had a few issues with one of my FTP backup suppliers. I’ve had to reset the account details several times in the last 8 weeks. I had to do it again recently, and i wasn’t going to be changing it on every single account manually anymore.
Now I couldn’t use the MainWP plugin as a lot of these have custom remote paths which are not tokenable, so I needed another way that could leave everything else but update what I needed, i.e the password, and I came up with the following script.
It was simple for me to set it up, set the username I was looking for and the Password to change it to and boom everysite was updated in about a minuite and I have it for next time.
/**
* MainWP snippet – smart update of Updraft FTP settings
*
* - No instance key required
* - Updates matching instances (or ALL if no username filter)
* - Only overwrites fields you specify
*/
/* ===== CONFIG ===== */
$action = 'update'; // 'add' or 'update'
$option_name = 'updraft_ftp';
// Only update instances where "user" matches this value.
// Leave '' to update ALL instances.
$require_existing_username = 'yourusername';
// Only include fields you WANT to update:
$new_values = array(
// 'host' => 'newhost.com',
// 'user' => 'newuser',
//'pass' => 'newpass',
// 'path' => '/new/path/',
// 'passive' => '1',
);
/* ===== END CONFIG ===== */
$site = home_url();
$existing = get_option( $option_name, null );
// Debug: show what we loaded
// (You can comment this block out once you know it's working.)
echo "Loaded option '{$option_name}' on {$site}:<br><pre>";
var_export( $existing );
echo "</pre><br>";
if ( 'add' === $action ) {
if ( $existing !== null && $existing !== false ) {
echo "SKIPPED: option exists on {$site}";
return;
}
// Add a NEW instance, generating a fresh key
if ( ! function_exists( 'wp_generate_password' ) ) {
require_once ABSPATH . 'wp-includes/pluggable.php';
}
$instance_key = 's-' . wp_generate_password(32, false, false);
$settings = array(
'instance_enabled' => '1',
'instance_conditional_logic' => array('type' => ''),
'host' => isset($new_values['host']) ? $new_values['host'] : '',
'user' => isset($new_values['user']) ? $new_values['user'] : '',
'pass' => isset($new_values['pass']) ? $new_values['pass'] : '',
'path' => isset($new_values['path']) ? $new_values['path'] : '',
'passive' => isset($new_values['passive']) ? $new_values['passive'] : '1',
);
$new_option = array(
'version' => '1',
'settings' => array(
$instance_key => $settings
)
);
$ok = update_option( $option_name, $new_option, 'yes' );
echo $ok ? "ADDED new instance on {$site}" : "ERROR: failed to add option on {$site}";
return;
}
if ( 'update' === $action ) {
if ( !is_array($existing) || !isset($existing['settings']) || !is_array($existing['settings']) ) {
echo "SKIPPED: option missing or malformed on {$site}";
return;
}
$updated = 0;
foreach ( $existing['settings'] as $key => &$instance ) {
if ( !is_array($instance) ) {
continue;
}
// Username filter
if ( !empty($require_existing_username) ) {
$current_user = isset($instance['user']) ? $instance['user'] : '';
if ( $current_user !== $require_existing_username ) {
continue;
}
}
// Update only the specified fields
foreach ($new_values as $field => $val) {
$instance[$field] = $val;
}
$updated++;
}
unset($instance); // break reference
if ( $updated > 0 ) {
$ok = update_option( $option_name, $existing );
echo $ok
? "UPDATED {$updated} instance(s) on {$site}"
: "ERROR: failed to update option on {$site}";
} else {
echo "NO MATCHING INSTANCES on {$site}";
}
return;
}
echo "ERROR: invalid action '{$action}'";
PHP