Created
January 8, 2015 20:01
-
-
Save bappi-d-great/9db40bab8e6909772e81 to your computer and use it in GitHub Desktop.
Replace URL in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'admin_menu', 'atr_menu' ); | |
function atr_menu() { | |
add_options_page( 'ATR Plugin Options', 'ATR Convert', 'manage_options', 'atr-convert', 'atr_options' ); | |
} | |
function atr_options() { | |
if ( !current_user_can( 'manage_options' ) ) { | |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); | |
} | |
if( isset( $_POST['option_save'] ) ){ | |
$options = $_POST; | |
update_option( 'atr_options', $options ); | |
wp_redirect( admin_url( 'options-general.php?page=atr-convert&msg=Data+saved.' ) ); | |
} | |
$options = get_option( 'atr_options', true ); | |
if( ! isset( $options ) || $options['newurl'] == '' ) | |
$options['options']['newurl'] = get_site_url(); | |
if( isset( $_REQUEST['replace_url'] ) && $_REQUEST['replace_url'] == 'now' ){ | |
global $wpdb; | |
$db = "Tables_in_" . DB_NAME; | |
$tables = $wpdb->get_results( 'SHOW TABLES' ); | |
foreach( $tables as $q ){ | |
$table = $q->$db; | |
$cols = $wpdb->get_results( 'SHOW COLUMNS FROM ' . $table ); | |
$query = "UPDATE $table SET "; | |
foreach( $cols as $col ){ | |
$query .= $col->Field . " = REPLACE({$col->Field}, '{$options['options']['oldurl']}/', '{$options['options']['newurl']}/'), "; | |
} | |
$query = rtrim( $query, ", " ); | |
$wpdb->query( $query ); | |
} | |
wp_redirect( admin_url( 'options-general.php?page=atr-convert&msg=All+URLs+are+replaced.' ) ); | |
} | |
?> | |
<div class="wrap"> | |
<?php if( isset( $_REQUEST['msg'] ) && $_REQUEST['msg'] != '' ) { ?> | |
<div class="updated"> | |
<p> | |
<?php echo str_replace( '+', ' ', $_REQUEST['msg'] ); ?> | |
</p> | |
</div> | |
<?php } ?> | |
<form action="<?php echo admin_url( 'options-general.php?page=atr-convert&noheader=true' ) ?>" method="post"> | |
<table cellpadding="5" cellspacing="5"> | |
<tr> | |
<th>Old URL (without trailing slash)</th> | |
<td><input type="text" name="options[oldurl]" value="<?php echo isset( $options['options']['oldurl'] ) && $options['options']['oldurl'] != '' ? $options['options']['oldurl'] : '' ?>"></td> | |
</tr> | |
<tr> | |
<th>New URL (without trailing slash)</th> | |
<td><input type="text" name="options[newurl]" value="<?php echo $options['options']['newurl'] ?>"></td> | |
</tr> | |
</table> | |
<p><input type="submit" class="button button-primary" name="option_save" value="Save" style="width: 100px; text-align: center;"></p> | |
</form> | |
<p><a class="button button-primary" href="<?php echo admin_url( 'options-general.php?page=atr-convert&replace_url=now&noheader=true' ) ?>">Replace</a></p> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment