Export Import WordPress Page
December 9, 2024 ยท AI Conversations
—
### **Exporting a Page by ID**
1. **Install and Activate a Plugin (Optional):**
– If you're not comfortable with manual methods, install a plugin like **WP All Export** or **Advanced Custom Fields Exporter**.
2. **Manually Export via WordPress Admin:**
– Go to **Tools > Export**.
– Select **Pages** and use the filters to specify the page ID.
– If no filter exists for IDs, consider exporting all pages, then editing the exported XML file.
3. **Manually Export via PHP (Code-Based):**
– Add the following snippet to your `functions.php` or a custom plugin:
function export_page_by_id($page_id) {
$page = get_post($page_id);
if ($page) {
header('Content-Type: application/json');
header('Content-Disposition: attachment; filename="page-' . $page_id . '.json"');
echo json_encode($page);
exit;
} else {
wp_die('Page not found.');
}
}
// Call the function via URL: yoursite.com/?export_page=PAGE_ID
if (isset($_GET['export_page'])) {
export_page_by_id(intval($_GET['export_page']));
}
Replace `PAGE_ID` with the ID of the page to export.
4. **Export Content via Database Query (Advanced):**
– Use a tool like phpMyAdmin or WP-CLI to export the specific page data from the `wp_posts` table:
SELECT * FROM wp_posts WHERE ID = PAGE_ID;
—
### **Importing the Page**
1. **Import XML or JSON:**
– If you exported the page as XML or JSON, use **Tools > Import** in the WordPress admin to upload the file.
2. **Manually Import via PHP (Code-Based):**
– Add a function in `functions.php` to process the JSON file and create a page:
function import_page($json_file_path) {
$page_data = json_decode(file_get_contents($json_file_path), true);
$new_page = [
'post_title' => $page_data['post_title'],
'post_content' => $page_data['post_content'],
'post_status' => 'publish',
'post_type' => 'page',
];
wp_insert_post($new_page);
}
// Usage: import_page('/path/to/page.json');
3. **Database Import (Advanced):**
– If you exported directly from the database, insert the exported row back into the `wp_posts` table of the target site, ensuring no ID conflicts.
—
### **Important Notes:**
– **Dependencies:** If the page relies on custom fields, taxonomies, or media, ensure these are also exported and imported. Use tools like **WP All Import** to handle complex dependencies.
– **Backup First:** Always back up your WordPress sites before performing exports or imports.
– **Content Validation:** After import, review the content to ensure everything (images, links, etc.) is intact.