
Creating a WordPress migration plugin involves several steps to ensure a smooth transfer of content, settings, and files from one WordPress site to another. Below is a basic outline and sample code to help you create a simple WordPress migration plugin.
Step 1: Create the Plugin Directory and File
- Go to the
wp-content/plugins/
directory of your WordPress installation. - Create a new folder for your plugin, e.g.,
simple-migration
. - Inside this folder, create a main PHP file, e.g.,
simple-migration.php
.
Step 2: Write the Plugin Header
Open the simple-migration.php
file and add the following code to handle the plugin header:
<?php /** * Plugin Name: Simple Migration * Description: A simple plugin to migrate data from one WordPress site to another. * Version: 1.0 * Author: Your Name * Author URI: Your Website URL */ // Exit if accessed directly. if (!defined('ABSPATH')) { exit; }
Step 3: Create Migration Functionality
Here’s a basic example that allows you to export posts and import them into another WordPress site. You will need to create additional functions for media files and settings as needed.
Export Functionality
Add the following code to handle the export of posts:
function sm_export_posts() { $args = array( 'numberposts' => -1, 'post_type' => 'post', ); $posts = get_posts($args); $data = []; foreach ($posts as $post) { $post_data = [ 'ID' => $post->ID, 'post_title' => $post->post_title, 'post_content' => $post->post_content, 'post_date' => $post->post_date, 'post_status' => $post->post_status, ]; $data[] = $post_data; } // Create a JSON file as the export file $json_file = fopen(plugin_dir_path(__FILE__) . 'exported-posts.json', 'w'); fwrite($json_file, json_encode($data)); fclose($json_file); return 'Exported posts successfully to exported-posts.json'; } // Add a shortcode to initiate export function sm_export_shortcode() { if (is_user_logged_in()) { return '<a href="' . admin_url('admin-post.php?action=sm_export') . '">Export Posts</a>'; } return 'You must be logged in to export posts.'; } add_shortcode('sm_export', 'sm_export_shortcode'); // Create a function to handle the export request function sm_handle_export() { $result = sm_export_posts(); wp_redirect( home_url() ); // Redirect to home after exporting exit; } add_action('admin_post_sm_export', 'sm_handle_export');
Import Functionality
Add the following code to handle the import of posts:
function sm_import_posts($file) { $json_data = file_get_contents($file); $posts = json_decode($json_data, true); foreach ($posts as $post) { // Prepare post data $new_post = array( 'post_title' => wp_strip_all_tags($post['post_title']), 'post_content' => $post['post_content'], 'post_status' => $post['post_status'], 'post_date' => $post['post_date'], 'post_type' => 'post', ); // Insert the post into the database wp_insert_post($new_post); } return 'Imported posts successfully.'; } // Import shortcode function sm_import_shortcode() { // File name for testing $file_path = plugin_dir_path(__FILE__) . 'exported-posts.json'; if (file_exists($file_path)) { $result = sm_import_posts($file_path); return $result; } return 'No export file found.'; } add_shortcode('sm_import', 'sm_import_shortcode');
Step 4: Using the Plugin
- Activate your plugin: Go to the WordPress admin panel, navigate to Plugins, and activate
Simple Migration
. - Export Posts: Insert the shortcode
[sm_export]
into any post or page where you want to place the export link. - Import Posts: Insert the shortcode
[sm_import]
into any post or page where you want to test the import functionality.
Step 5: Complete the Plugin
This basic example helps to export and import posts. However, for a robust migration plugin, you should also consider:
- Exporting and importing custom post types.
- Migrating media files (uploads).
- Copying user settings and configurations.
- Handling taxonomies (categories/tags).
- Providing a more user-friendly interface with an admin page.
This is a basic migration plugin that allows you to export and import posts using JSON. Enhancing the plugin with additional features and a user interface will provide a more comprehensive migration solution. Be sure to test thoroughly in a staging environment before using it on a live site. Additionally, consider implementing error handling and security measures, such as nonce verification, for better protection.