- b2evolution CMS Support Forums
- b2evolution Support
- General Support
- Script to migrate to Wordpress
1 nomad Jul 18, 2024 22:05
With the help of ChatGPT I was able to migrate posts, comments and users to a Wordpress site. After revising the script 12 times after error messages, we made it. But maybe the experts among you can have a look?
<?php
// Database connection settings for b2evolution
$b2e_db_host = 'localhost';
$b2e_db_user = 'b2evolution_user';
$b2e_db_pass = 'b2evolution_password';
$b2e_db_name = 'b2evolution_db';
// Database connection settings for WordPress
$wp_db_host = 'localhost';
$wp_db_user = 'wordpress_user';
$wp_db_pass = 'wordpress_password';
$wp_db_name = 'wordpress_db';
// Connect to b2evolution database
$b2e_conn = new mysqli($b2e_db_host, $b2e_db_user, $b2e_db_pass, $b2e_db_name);
if ($b2e_conn->connect_error) {
die("Connection to b2evolution database failed: " . $b2e_conn->connect_error);
}
// Connect to WordPress database
$wp_conn = new mysqli($wp_db_host, $wp_db_user, $wp_db_pass, $wp_db_name);
if ($wp_conn->connect_error) {
die("Connection to WordPress database failed: " . $wp_conn->connect_error);
}
// Helper function to convert dates to WordPress format
function convert_date($date) {
return date('Y-m-d H:i:s', strtotime($date));
}
// Function to sanitize slugs
function sanitize_title($title) {
return preg_replace('/[^a-z0-9]+/i', '-', strtolower(trim($title)));
}
// Function to hash passwords for WordPress
function wp_hash_password($password) {
return password_hash($password, PASSWORD_BCRYPT);
}
// Import posts
echo "Importing posts...\n";
$b2e_posts_query = "SELECT post_ID, post_title, post_content, post_status, post_datestart FROM evo_items__item";
$b2e_posts_result = $b2e_conn->query($b2e_posts_query);
if ($b2e_posts_result && $b2e_posts_result->num_rows > 0) {
while ($row = $b2e_posts_result->fetch_assoc()) {
$status = ($row['post_status'] == 'published') ? 'publish' : 'draft';
$post_date = convert_date($row['post_datestart']);
// Check if post already exists
$wp_check_post_query = $wp_conn->prepare("SELECT ID FROM wp_posts WHERE ID = ?");
$wp_check_post_query->bind_param("i", $row['post_ID']);
$wp_check_post_query->execute();
$wp_check_post_query->store_result();
if ($wp_check_post_query->num_rows > 0) {
// Update existing post
$wp_posts_query = $wp_conn->prepare("UPDATE wp_posts SET post_title = ?, post_content = ?, post_excerpt = '', post_status = ?, post_date = ?, post_date_gmt = ?, post_modified = ?, post_modified_gmt = ?, to_ping = '', pinged = '', post_content_filtered = '' WHERE ID = ?");
$wp_posts_query->bind_param("sssssssi", $row['post_title'], $row['post_content'], $status, $post_date, $post_date, $post_date, $post_date, $row['post_ID']);
} else {
// Insert new post
$wp_posts_query = $wp_conn->prepare("INSERT INTO wp_posts (ID, post_title, post_content, post_excerpt, post_status, post_date, post_date_gmt, post_modified, post_modified_gmt, to_ping, pinged, post_content_filtered) VALUES (?, ?, ?, '', ?, ?, ?, ?, ?, '', '', '')");
$wp_posts_query->bind_param("isssssss", $row['post_ID'], $row['post_title'], $row['post_content'], $status, $post_date, $post_date, $post_date, $post_date);
}
if (!$wp_posts_query->execute()) {
echo "Error importing post ID {$row['post_ID']}: " . $wp_posts_query->error . "\n";
}
}
echo "Posts imported successfully.\n";
} else {
echo "No posts found in b2evolution database or query failed: " . $b2e_conn->error . "\n";
}
// Import categories
echo "Importing categories...\n";
$b2e_categories_query = "SELECT cat_ID, cat_name, cat_description FROM evo_categories";
$b2e_categories_result = $b2e_conn->query($b2e_categories_query);
if ($b2e_categories_result && $b2e_categories_result->num_rows > 0) {
while ($row = $b2e_categories_result->fetch_assoc()) {
$slug = sanitize_title($row['cat_name']);
// Check if term already exists
$wp_terms_query = $wp_conn->prepare("INSERT INTO wp_terms (term_id, name, slug) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), slug = VALUES(slug)");
$wp_terms_query->bind_param("iss", $row['cat_ID'], $row['cat_name'], $slug);
if (!$wp_terms_query->execute()) {
echo "Error importing category ID {$row['cat_ID']}: " . $wp_terms_query->error . "\n";
}
$wp_term_taxonomy_query = $wp_conn->prepare("INSERT INTO wp_term_taxonomy (term_id, taxonomy, description) VALUES (?, 'category', ?) ON DUPLICATE KEY UPDATE taxonomy = VALUES(taxonomy), description = VALUES(description)");
$description = !empty($row['cat_description']) ? $row['cat_description'] : '';
$wp_term_taxonomy_query->bind_param("is", $row['cat_ID'], $description);
if (!$wp_term_taxonomy_query->execute()) {
echo "Error importing category taxonomy ID {$row['cat_ID']}: " . $wp_term_taxonomy_query->error . "\n";
}
}
echo "Categories imported successfully.\n";
} else {
echo "No categories found in b2evolution database or query failed: " . $b2e_conn->error . "\n";
}
// Import comments
echo "Importing comments...\n";
$b2e_comments_query = "SELECT comment_ID, comment_item_ID, comment_author, comment_author_email, comment_content, comment_date FROM evo_comments";
$b2e_comments_result = $b2e_conn->query($b2e_comments_query);
if ($b2e_comments_result && $b2e_comments_result->num_rows > 0) {
while ($row = $b2e_comments_result->fetch_assoc()) {
$comment_date = convert_date($row['comment_date']);
// Check if comment already exists
$wp_check_comment_query = $wp_conn->prepare("SELECT comment_ID FROM wp_comments WHERE comment_ID = ?");
$wp_check_comment_query->bind_param("i", $row['comment_ID']);
$wp_check_comment_query->execute();
$wp_check_comment_query->store_result();
if ($wp_check_comment_query->num_rows > 0) {
// Update existing comment
$wp_comments_query = $wp_conn->prepare("UPDATE wp_comments SET comment_author = ?, comment_author_email = ?, comment_content = ?, comment_date = ?, comment_date_gmt = ? WHERE comment_ID = ?");
$wp_comments_query->bind_param("sssssi", $row['comment_author'], $row['comment_author_email'], $row['comment_content'], $comment_date, $comment_date, $row['comment_ID']);
} else {
// Insert new comment
$wp_comments_query = $wp_conn->prepare("INSERT INTO wp_comments (comment_ID, comment_post_ID, comment_author, comment_author_email, comment_content, comment_date, comment_date_gmt) VALUES (?, ?, ?, ?, ?, ?, ?)");
$wp_comments_query->bind_param("iisssss", $row['comment_ID'], $row['comment_item_ID'], $row['comment_author'], $row['comment_author_email'], $row['comment_content'], $comment_date, $comment_date);
}
if (!$wp_comments_query->execute()) {
echo "Error importing comment ID {$row['comment_ID']}: " . $wp_comments_query->error . "\n";
}
}
echo "Comments imported successfully.\n";
} else {
echo "No comments found in b2evolution database or query failed: " . $b2e_conn->error . "\n";
}
// Import users
echo "Importing users...\n";
$b2e_users_query = "SELECT user_ID, user_login, user_email, user_pass FROM evo_users";
$b2e_users_result = $b2e_conn->query($b2e_users_query);
if ($b2e_users_result && $b2e_users_result->num_rows > 0) {
while ($row = $b2e_users_result->fetch_assoc()) {
// Rehash passwords for WordPress
$hashed_password = wp_hash_password($row['user_pass']);
$wp_users_query = $wp_conn->prepare("INSERT INTO wp_users (ID, user_login, user_email, user_pass) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE user_login = VALUES(user_login), user_email = VALUES(user_email), user_pass = VALUES(user_pass)");
$wp_users_query->bind_param("isss", $row['user_ID'], $row['user_login'], $row['user_email'], $hashed_password);
if (!$wp_users_query->execute()) {
echo "Error importing user ID {$row['user_ID']}: " . $wp_users_query->error . "\n";
}
}
echo "Users imported successfully.\n";
} else {
echo "No users found in b2evolution database or query failed: " . $b2e_conn->error . "\n";
}
// Close database connections
$b2e_conn->close();
$wp_conn->close();
echo "Migration completed.\n";
?>
Thank you :)