How to Import Posts from a Text File into WordPress

Recently, I was converting an old site that used cute news to wordpress. Here is a simple method that you can use to import news, posts or content from a text file into wordpress.

This example uses cute news, however the implementation is very similar for any similar import script. This script reads from a text data file but you can just as easily use an RSS feed or database.

This script assumes the text data file has each post on a separate line with fields separated by a |. This is how most flat file news scripts store data. It also uses the php tidy extension to clean the input before inserting into the database.

If you need to import data from a different source, all you need to do is include the wp-load.php script and use wp_insert_post to insert post.

 

27 thoughts on “How to Import Posts from a Text File into WordPress

  • Francesco says:

    Thank you a lot! This is exactly what I needed! I just tried it and it worked.

    Unfortunately there’s no tidy on my server (and I’m not sure I have enought rights to install it, I’ll check), so I commented out the lines that used it.

    I noticed that when there are accented letters in the post, it gets truncated… is it because I removed the tidy part?

    Great tool, anyway!

    • Greg says:

      Yes, wordpress truncates your posts if you don’t clean up your input, it doesn’t like raw characters like accents. WordPress has it’s own libraries for this, but I am familar with html tidy so I just use that.

  • Sen says:

    This is amazing. I could hug you.

  • Meigo Tammsaar says:

    I have a small project in progress and I wanna import all the posts from Automatweb that is an old content management system that was invented by Estonian programmers. The ftp and database is so hard to understand that I cannot locate the files where the news are in. Maybe you guys can give me some advice in this.

    …aah, another question. How to imports users from Joomla to WP?

  • wallace says:

    Hi Greg,
    This actually looks promising, but I’ve got no idea how to actually use this.
    Where am I supposed to insert this PHP code? And what am I supposed to do afterwards?

    Thanks in advance.

    • Greg says:

      Sorry for the late reply, what are you trying to import? If you haven’t solved your issue yet, let me know.

      • wallace says:

        Hi, and thanks for replying!

        I’m trying to import Cutenews posts from a system I installed a few years ago into a new site based on WordPress I’m currently building.

        • Greg Freeman says:

          Hi, I wrote the above code to work with cutenews, what happens when you try to run it. The above code will import titles, posts, dates etc into wordpress but disregard author and category. All you need to do is change the two constant values up the top to point to your wordpress directory and cutenews file. This code also uses the php tidy extension to clean up posts before importing.

          • wallace says:

            That means I just have to copy this code to an empty PHP file, change the essential fields and access the file somewhere on the server?
            The reason I’m not trying it, is because I’m afraid to screw up my WordPress database ^^

  • George says:

    I want to copy paste my spreadsheet data in Excel into WordPress, what I suppose to do ? Thanks for your information and help

  • George says:

    There is my website above, is it available to copy paste a spreadsheet data from Excel into my website above ? Thanks for your help.

    • Greg says:

      Hi George, there is php extensions for reading/writing excel data, you should use those to read your files and then you would be able to use the script above.

  • Marcos says:

    Hello,

    What format should we use in the .txt files?

    title|post|date|image ?

    Thanks in advance

  • Erick says:

    I do not get this one bit, how exactly is this script used? What are the exact instructions on how to use this? I am trying to import fusion news (4 years worth) into my wordpress. You say you wp-load and insert post but you don’t say how exactly especially for people who are new to all of this.

    Do we create that code as a seperate php file and run it? How on earth do we work this? Is it an sql code? Help us please I need this!

    • Greg Freeman says:

      wp-load.php is in the main wordpress directory. You only need to edit the two constants at the top. Specify the directory that wordpress is in as well as the location of the cute news text file.

  • Francis says:

    Hi Greg,

    This looks exactly like what I’m looking for at the moment, to switch my website (AshleyTisdale.org) from cutenews to wordpress. The other site’s webmiss and I are trying to figure out where to insert the code you posted above, and if there’s anything we need to edit/add.

    Hopefully you’ll see this comment and reply back as soon as possible! xx

  • Gary says:

    I may be being rather think here but I can’t understand what it is I need to do. I am trying to convert an old cutenews site to wordpress. Where do I put the above script ?

    Can anyone who has successfully done it post a how to ??

    Many Thanks

  • Tim Dawson says:

    Thank you for this. I’m trying to make it work for a purely text file. It sort of works, but the result is full of errors/warnings like:
    “Array ( [post_title] => tidy Object ( [errorBuffer] => line 1 column 1 – Warning: missing declaration line 1 column 1 – Warning: plain text isn’t allowed in elements line 1 column 1 – Info: previously mentioned line 1 column 1 – Warning: inserting implicit line 1 column 1 – Warning: inserting missing ‘title’ element [value] => 10/11/2003 )”
    The title really IS intended to be the date as text. It would seem the CUTENEWS format isn’t pure text, but I haven’t yet worked out what the program is expecting.

  • Tim Dawson says:

    Further investigation suggests that the warnings are caused by tidy not finding , , etc. so they don’t actually affect things. Just a bit inelegant.

    The warnings are seen by adding the following final line:
    “print_r($new_post); echo ”;”

  • Pete says:

    @Greg I’m using wp_insert_post then a few update_field functions from a front end form. Is there a way to force accent characters into a custom field? WordPress doesn’t insert any accent chars when using wp_insert_post but if I save and updates post meta in the backend it stores fine.

  • […] won’t take credit for someone else’s work so I will simply link to Greg Freeman’s article about migrating from Cutenews To WordPress. But I had to create an install of WordPress on my local […]

  • jiwan says:

    It solves my problem.
    And I have feedback for you.
    I clicked on navigation menu (About Me). On first click I am not able to go to that page. On second time, I clicked above text and i went to that page.

  • Nainish says:

    is there any plugin to show txt file’s contains in a page via externel url or upload files?

  • Milan says:

    Worked like a charm, thank you!

  • Marie Drolet says:

    Hi, We have started to build a website with Elegant Theme. The challenge arises as we would like to automatically post and update .txt files data into our real estate website. The use of IDX is prohibited as we do not want our listings to be distributed out of our Greater Montreal Real Estate Board and our website. Our Board supplies us with the zip file that contains text files (.txt), by depositing it directly into our FTP. How can import this file into our database and insure it is updated regularly as the files are updated daily? Our visible website is just a temporary dummy one. Thanks, Mary

  • Rudy says:

    oh man, at last, this works !I have an old site with Cutenews on it, trough this tips i get all the news from cutenews to wordpress, THANK YOU!

  • This is a nifty bit of code. I needed it to import a bunch of text file articles from a library of 100000 articles.  The articles have various formats but most follow similar patterns.  The sections of the articles are broken up with titles such as Title: , Summery:, Story:, Word Count:, Tags: and some others.

    I tested one article as is and the code did not work. Gave me errors. So I had to edit the file and put in | between each section and remove the titles of each section and all the paragraph breaks.  I also had to calculate the linux time stamp code by asking google what the linux time code was so i could put it in as the first section and the follow it with a | then put in the title|summery|story article content|image format.

    SO my next challenge will be to rewrite the php code to parse the raw text file and look for the regx patterns to populate the array that gets past to $post_id = wp_insert_post($new_post); with the right info so I don’t have to edit each article and modify it with adding |’s to deliniate the sections. Then I need to create this as function so I can pass an article url to it that is called recursively from the article library directory’s automatically.  The articles are in a nested folder structure with the folder names being a good general category name with sub folders being good tag names and nested some times 3 or 4 levels deep.

Leave a Reply to wallace Cancel reply

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