Greg Freeman's Blog

PHP, Linux Tips

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.

 

20 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.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">