Created
February 7, 2024 20:55
-
-
Save larbous/0d13f99b871f2e6e730176c9417f2e98 to your computer and use it in GitHub Desktop.
Importando dados do MS-Access para a tabela users do 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 | |
// Conexão com o banco de dados MS-Access | |
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=Caminho/Para/Seu/Arquivo.mdb"); | |
// Consulta para obter os usuários do MS-Access | |
$query = "SELECT * FROM usuarios"; | |
$result = $db->query($query); | |
// Loop através dos resultados | |
foreach ($result as $row) { | |
// Criar um novo usuário no WordPress | |
$user_id = wp_insert_user([ | |
'user_login' => $row['username'], | |
'user_pass' => $row['password'], | |
'user_email' => $row['email'], | |
'display_name' => $row['display_name'], | |
]); | |
// Verificar se o usuário foi criado com sucesso | |
if (!is_wp_error($user_id)) { | |
// Adicionar campos personalizados ao usuário | |
update_user_meta($user_id, 'bio', $row['bio']); | |
update_user_meta($user_id, 'especificacoes', $row['especificacoes']); | |
// Fazer upload do avatar para a biblioteca de mídia | |
$avatar_url = $row['avatar_url']; | |
$avatar_file = download_url($avatar_url); | |
if (!is_wp_error($avatar_file)) { | |
$file_name = basename($avatar_file); | |
$attachment = [ | |
'guid' => $avatar_file, | |
'post_mime_type' => wp_check_filetype($file_name)['type'], | |
'post_title' => $file_name, | |
'post_content' => '', | |
'post_status' => 'inherit', | |
]; | |
$attachment_id = wp_insert_attachment($attachment, $avatar_file); | |
if (!is_wp_error($attachment_id)) { | |
require_once ABSPATH . 'wp-admin/includes/image.php'; | |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $avatar_file); | |
wp_update_attachment_metadata($attachment_id, $attachment_data); | |
update_user_meta($user_id, 'avatar', $attachment_id); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment