最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to rename custom table name programatically in wordpress?

programmeradmin1浏览0评论

in db there are custom table name like wp_wpdf_docs and want to change table name to wp_ccdm_docs

as i am using

    $oldtable_name = $wpdb->prefix . "wpfd_docs"; 

    $newtable_name = $wpdb->prefix . "ccdm_docs"; 

    $rename=  $wpdb->query("RENAME TABLE ".$oldtable_name ." TO " .$newtable_name); 

is it right process?

in db there are custom table name like wp_wpdf_docs and want to change table name to wp_ccdm_docs

as i am using

    $oldtable_name = $wpdb->prefix . "wpfd_docs"; 

    $newtable_name = $wpdb->prefix . "ccdm_docs"; 

    $rename=  $wpdb->query("RENAME TABLE ".$oldtable_name ." TO " .$newtable_name); 

is it right process?

Share Improve this question edited Jan 4, 2022 at 21:24 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jan 4, 2022 at 11:31 Kou WPKou WP 53 bronze badges 1
  • 1 Can't you test it locally to see if it works? The big question will be: Where is that table used? If the old name is referenced after the rename, that will run you into trouble. – kero Commented Jan 4, 2022 at 11:45
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can use the query function to rename tables.

global $wpdb;
$oldTable = $wpdb->prefix . "wpfd_docs";
$newTable = $wpdb->prefix . "ccdm_docs"; 
$renameOk = $wpdb->query("RENAME TABLE " . $oldTable . " TO " . $newTable);
if( $renameOk )
{
    echo 'All good';
}else{
    echo 'Failed to rename table. Last DB error: ' . $wpdb->last_error;
}
发布评论

评论列表(0)

  1. 暂无评论