提问者:小点点

在WordPress中创建新用户时创建表


WordPress有一个管理仪表板。在仪表板中,我们可以作为管理员添加新用户。我想在管理员添加新用户时在MySQL中创建一个表。例如,我创建了一个名为John Smith的用户,其用户名为user1;当我成功添加该用户时,将在名为user1的数据库中创建一个表。


共1个答案

匿名用户

我认为你需要这样尝试

add_action('user_register', 'custom_user_table_save', 10, 1);
function custom_user_table_save($user_id){
   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   global $wpdb;
   // get newly created username and create new table into DB
   $tablename = get_userdata($user_id)->user_login; 
   $table_create = "CREATE TABLE " . $tablename";";
   //  $wpdb->prefix want to add default WP db prefix to custom table otherwise remove it
   maybe_create_table( $wpdb->prefix . $tablename, $table_create );
}