smack-logo

How to import CSV files in WordPress without a plugin

  • Blog
  • How to import CSV files in WordPress without a plugin
import-CSV-files-in-WordPress-without-plugin

If you’re planning to transfer content to your WordPress site and wondering if it’s possible to do so without a plugin, then the answer is a resounding yes!

Fortunately, WordPress offers several built-in options that allow you to import without relying on a third-party plugin. Moreover, it also supports various file types that cater to your specific needs.

In this tutorial, we’ll guide you through the step-by-step process of importing programmatically to WordPress without the use of a third-party plugin.

Warning 1: Backup Before You Proceed

It is always important to backup your data regularly before you do something that updated the database whether you use a plugin or not. This ensures that you have a safe backup copy of your website’s content and database so that you can always rollback to a previous version in case of any issues, data loss or corruption. 

Warning 2: Potential Risks Involved

Importing content without a plugin can potentially cause issues with theme or other plugins, and break the functionality or design issues. It is also increase the risk of security vulnerabilities if the content with malicious code or scripts get injected while importing. 

How to use  WordPress functions

Do you want to import a bunch of posts into your site but don’t want to do it manually one by one? Well, you’re in luck because I have a solution for you!

One way to import posts as CSV is by using the built-in wp function called wp_insert_post(). This function allows you to programmatically create and insert them into the database.

Here’s how you can use this function:

First, make sure your file is uploaded to your server or host through ssh, cpanel, ftp or sftp, for example in the directory /var/www/html/wp-content/uploads/posts.csv.

Next, add the following code snippet to your theme’s functions.php file e.g: \wp-content\themes\twentytwentythree\functions.php

if (($handle = fopen("/var/www/html/feb/wordpress/wp-content/uploads/smack_uci_uploads/imports/d107020a28796c63d6984ad91f0fcab5/post.csv", "r")) !== FALSE) {
	// Read the CSV file line by line
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
		
	  // Create a new post object and set its properties
	  $post = array(
		'post_title' => $data[0],
		'post_content' => $data[1],
		'post_status' => 'publish',
		'post_type' => 'post',
	  );
  
	  // Insert the post into the database
	  $post_id = wp_insert_post($post);
  
	  // Check if the post was inserted successfully
	  if ($post_id > 0) {
		// Post was inserted successfully, do something else if needed
	  } else {
		// Post was not inserted, handle the error if needed
	  }
	}
	fclose($handle);
  }

Save the changes to your functions.php file.

You can also edit the theme file from Appearance > Theme File Editor

wordpress-theme-file-editor
When you run a page  URL in the browser, the import will be triggered and processed automatically. You can view from the wp-admin panel to check if all the records  were  inserted successfully.

That’s it! You are done now. .

Keep in mind that this is just a basic example, and you can modify the code to suit your specific needs. For example, you can add additional code to handle custom post types, taxonomies, or other properties.

Also, you can customize to use the code snippet in any filter or action hooks based on your trigger function.

If you need more help or would like to see more use cases, additional examples and  code snippets, do not  hesitate to reach out to us! You can leave a comment below or use the contact form, and we’ll be happy to update the tutorial with more information to help you out.

 

How to use  the wp-cli method

Another method to import users as CSV into WordPress is by using wp-cli. This method is pretty straightforward and easy to follow. First, you need to prepare a the file in a specific format, which should look something like this:

user_login,user_email,display_name,role,authors_parameter
Johndoe,[email protected],John Doe,contributor,create
smacksupport,[email protected],smackcoders,administrator,create
jiju2fabio,[email protected],jiju2fabio,author,create

 

You can create or update your user CSV file to match this structure. If you want to use the same sample that we’ve used in this tutorial, you can download it for training purposes. Once you have your file ready, upload it to your uploads folder, and the path to the file should be /var/www/html/wp-content/uploads/users.csv.

Assuming you have already installed wp-cli, you can use the following WP-CLI command:

wp user import-csv /var/www/html/wp-content/uploads/users.csv

And that’s it! Your users should be imported now. Note that you can also upload the file from a remote URLs using this method.

However, it’s important to note that this method is only useful for users as  CSV. If you’re looking to import posts from CSV, the wp function method  is the better option. The wp-cli method is  only compatible  with WXR files, which can be complicated. So, if you want to import posts, it’s best to stick with the first method we discussed.

How to use the Built-in Options

First, log in to your wp-admin dashboard and go to the Import section under Tools. Scroll down until you see the WordPress option and click “Install now”. Once it’s installed, you’ll see a new option called “Run Importer”. Click on that to start the process.

buildin-run-importer-wordpress

 

Next, you’ll be prompted to upload the WXR (.xml) file. This is the file that contains all the content you want to bring to your site.

 

upload-wxr-file-all-import-wordpress

Once you’ve uploaded the file, you’ll see the “Assign Authors” section. Here, you can choose how to handle author information. You can import authors, create new users, or assign them to already available users. Make sure to review and adjust the settings as needed. You can also choose to download  any attachments that are included in the file.

Once you’ve configured all the settings, click “Submit” to start the process. The tool will process the WXR file and import the content. If any errors occur during the process, you’ll be notified so you can address them.

And that’s it! With this built-in tool, importing content has never been easier. No need for other plugins or custom code.

Know about Built-in WordPress All Import Options

Well, did you know that WordPress has other built-in options that you can use for this purpose?

To access these built-in tools, all you need to do is go to your WP-Admin dashboard, click on “Tools,” and then select “Import.” From there, you’ll see a table with different options available.

Depending on your needs, you can choose the option that suits you best. For example, if you want to migrate from a Blogger blog, you can use the Blogger option. If you want to import from an RSS feed, you can use the RSS option. And if you want to transfer  from an export file in WXR format, which supports posts, pages, comments, custom fields, categories, and tags, you can choose the WordPress run importer option.

However, keep in mind that these default tools have some limitations. You can only use them for a one-time process, and you cannot automate or schedule the process. Also, filters and partial imports may not work correctly, and there is no proper support or fixes available.

But don’t worry, if you need more robust and reliable tools, there are third-party plugins like WP Ultimate CSV Importer available that can help you out.

Here are some  plugins you can try

  • https://wordpress.org/plugins/one-click-demo-import/
  • https://wordpress.org/plugins/advanced-import/
  • https://wordpress.org/plugins/import-facebook-events

How to plan a simple migration

If you’re planning to import your data, it’s essential to plan well  before getting started. Let’s discuss some takeaways that can help you choose the right option.

Firstly, if you want to import user metadata as CSV, you can do it programmatically using wp-cli without the need for any plugin. Secondly, you can use the WP function method by customizing the shared code to your specific needs. And if you have WooCommerce installed and active, you can import products and tax details as CSV without needing another plugin.

If you’re planning to use  WXR, you can use the wp-cli method, but it requires more effort and time. The easiest way is to use the default importer option.

Moreover, there are other options available for bloggers, Tumblr, LiveJournal, Movable Type, TypePad, etc., and from an RSS feed as well. Understanding each option’s capabilities and limitations can help you choose the right option for your needs.

Also, It’s important to know about each file type that WordPress supports. Let me explain each file type in detail so that you can understand it better.

WXR: It stands for WordPress eXtended RSS. It is an XML-based file format that contains content, categories, and tags. It is the most common file format used for import  export data.

XML: It stands for eXtensible Markup Language. It is a file format that is used to store and transport data. WordPress uses XML to export and import content.

CSV: It stands for Comma Separated Values. It is a file format that stores data in a tabular form where each column is separated by a comma. CSV files are commonly used to store data like product information, user data, and more.

RSS: It stands for Really Simple Syndication. It is a file format used to publish frequently updated content. It also allows you to import RSS feeds into your site.

JSON: It stands for JavaScript Object Notation. It is a file format used to store and exchange data. So, those are the file types supported. It’s important to choose the right file format based on your data and requirements.

I hope this information helps you. Some useful References:

If you want to learn more about the WP-CLI method, you can visit the WP-CLI website.

For using the WP function method, you can use the wp_insert_post() function. This function allows you to programmatically insert data into your WordPress site. You can learn more about it from wp developer reference.

Lastly, if you want to use the WordPress default options , you can find it in the plugin repository. In conclusion, importing content can be made easy with the right tools and an understanding of the available options and knowing the limitations and capabilities of each option.

With these tips in mind, you can transfer  your content seamlessly and focus on creating quality content for your website.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.