Support #16621 » user-login-change.php
| 1 |
<?php
|
|---|---|
| 2 |
|
| 3 |
if ( empty( $args[0] ) || empty( $args[1] ) ) { |
| 4 |
WP_CLI::error( 'Too few arguments. Usage: wp eval-file /path/to/user-login-change.php from_username to_username' ); |
| 5 |
}
|
| 6 |
|
| 7 |
$from_username = $args[0]; |
| 8 |
$to_username = $args[1]; |
| 9 |
|
| 10 |
global $wpdb; |
| 11 |
|
| 12 |
// sanity checks
|
| 13 |
|
| 14 |
// user_login
|
| 15 |
$q = $wpdb->prepare( "UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $to, $from ); |
| 16 |
if ( false === $wpdb->query( $q ) ) { |
| 17 |
WP_CLI::error( 'Could not change the user_login value' ); |
| 18 |
}
|
| 19 |
|
| 20 |
// user_nicename
|
| 21 |
$q = $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %s WHERE user_nicename = %s", $to, $from ); |
| 22 |
if ( false === $wpdb->query( $q ) ) { |
| 23 |
WP_CLI::error( 'Could not change the user_nicename value' ); |
| 24 |
}
|
| 25 |
|
| 26 |
$replace_tables = [ |
| 27 |
buddypress()->activity->table_name, |
| 28 |
$wpdb->posts, |
| 29 |
$wpdb->comments |
| 30 |
];
|
| 31 |
|
| 32 |
// URL swap.
|
| 33 |
$members_base = bp_get_root_domain() . '/' . bp_get_members_root_slug() . '/'; |
| 34 |
$from_url = $members_base . $from_username . '/'; |
| 35 |
$to_url = $members_base . $to_username . '/'; |
| 36 |
|
| 37 |
$url_sr_command = "search-replace --all-tables $from_url $to_url " . implode( ' ', $replace_tables ); |
| 38 |
WP_CLI::runcommand( |
| 39 |
$url_sr_command, |
| 40 |
[ 'exit_error' => true ] |
| 41 |
);
|
| 42 |
|
| 43 |
// Mentions. Use regex to avoid replacing partials.
|
| 44 |
$from_mention = '@' . $from_username . '\\b'; |
| 45 |
$to_mention = '@' . $to_username; |
| 46 |
$mention_sr_command = "search-replace --regex --all-tables $from_username $to_username " . implode( ' ', $replace_tables ); |
| 47 |
WP_CLI::runcommand( |
| 48 |
$mention_sr_command, |
| 49 |
[ 'exit_error' => true ] |
| 50 |
);
|