How to migrate WordPress site with media library

Migrating along with the media library is tricky as there is no straightforward solution recently I’ve migrated successfully along with the media library

Here are the steps followed

Content Export and Import:

  • Tools – > Export – > All content
  • It’ll download XML file containing all the content of posts, but it’s all linked to old blog site.
  • Open the downloaded XML file and replace all old blog domains with new blog name and alo make sure it replaces all the image paths to match with https://domain name/wp-content/uploads/YYYY/MM/<image name>.png
  • Save and import the xml using tools – > import – > WordPress importer
  • Select the author name or choose the existing author from the dropdown.
  • Import content.

Media export and import:

  • Open the appearance – > theme file editor
  • theme editor
  • open functions.php and scroll to the bottom of the file and add below php function that would import images to selected folder and change the below year with your exported folder year and month.
  • Save the file (Once you are done remove the function and save).
  • Upload the exported media to Media library just like a new images from the specified month and year folder.
  • Repeat the same for all folders, fist change the function with folder year and month and then import files.
  • Once all completed, remove the function from functions.php and save.
// put this code into your function.php file
add_filter('upload_dir', function(){
	return _wp_upload_dir('2022/11');  // ('YEAR/MONTH')
}, 100, 0);

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks