Make WordPress static

Since I had so many WordPress sites to convert into static HTML, I decided to write a nice little bash script for it. This will take care of everything except editing the theme files to take out links to RSS, meta links, or anything else you want removed. It will basically go through the list of steps outlined in this post: Convert WP to Static HTML Part 2.  Just upload this script to your server (on the same level as wp-config.php) and run it as a command line program (ie: user@server $ ./wpstatic). Make sure to change the permissions on the file so that your user can execute it (ex. 744, or chmod u+x wpstatic).

What’s nice about the script is that it grabs the database info from the wp-config.php and autmagically formats the wget command. So basically, it does the following steps for you:

  1. Update the database to close comments on all posts and pages. Update the database permalink structure.
  2. Checks for a .htaccess file and for the correct rewrite code. If it doesn’t exist it creates one. If it exists but doesn’t think the right code is in there, it will append it to the end of the file.
  3. It makes a backup of the database before making the changes, and then again right after.
  4. Reminds you to change the theme files before asking if you would like it to run the wget command.

Ideally, you’ll have edited the theme files before hand, so that you can just run this script, and it will make a static copy of your WordPress blog.

Back by popular demand… I had to convert a lot of sites again, so I cleaned up the code and added some switches for skipping any of the steps.

Upload the script to your webhost/server where the WordPress files are located. Then run it on the terminal/PuTTY.

[code lang="bash"]./wpstatic [option][/code]

Downloads

Check the git repository for the latest version: https://github.com/mossiso/WP-Static

Download [wpstatic 1.2, 12/08/10]

Download [wpstatic 1.0, 8/13/09]

42 thoughts on “Make WordPress static

  1. Pingback: Bookmarks for October 27th from 10:15 to 14:39 « LostFocus

  2. Justin

    Sadly this script appears no longer able to download.

    It’s a shame as it would be useful, still will work through your manual steps.

    Thanks.

    Reply
    1. ammon Post author

      I was actually working on this script just before the holidays. I’ve had several requests since then, so I finally took the time to fix the link to the old script and the updated one.

      Reply
  3. Ben

    I’m not using your script but your tutorial was great and I’m using the information you provided all the time.

    I know nothing about wordpress and would look to use the page update hook to run the wget on that particular page.

    Any advice on where to start?

    Thanks,

    Ben

    Reply
    1. ammon Post author

      @Ben

      Glad the information was helpful.

      I’m not sure I follow your question, though. Do you mean a page update hook in WordPress? If so I would guess there is some kind of function you’ll have to create in the themes’ function.php file that would make an external call to wget using PHP’s exec() function, see (http://php.net/manual/en/function.exec.php).

      Hope that helps.

      Reply
  4. Jaryd

    Hey Ammon,

    Thanks so much for the awesome tutorials and script. I’m in in the process of converting an old wordpress blog to static html and need a little help getting started. When you say to run the script in the same directory as the wp-config.php file, do you mean from a terminal/command line on a local machine pointed at the hosted directory, or in an online terminal/command line?

    If I could figure the first step out, I’m sure the rest would fall into place!

    Thanks again,

    Jaryd

    Reply
    1. ammon Post author

      Hi Jaryd,

      Glad to be of help, and sorry for taking so long to respond.

      You’ll need to be logged into the remote server through a terminal/command line. So you’ll need to upload the script to the server, and then run it from there.

      That’s a great question, by the way. I’ve read so many tutorials that skip a basic step like that, and when trying to learn a new skill, that’s can be a huge step.

      Do let me know if you have any other questions.

      Reply
  5. Pingback: rolling your own and getting pwned | parezco y digo

  6. Joshua

    Hey Ammon,

    First off, thanks for the script. I’m having a bit of trouble when I try to run it; I get an error message:

    ./wpstatic: line 122: tables+=wp_commentmeta : command not found

    I checked the table and there was no content in wp_commentmeta—could this be the problem? It runs fine on -a.

    Thanks,
    Josh

    Reply
    1. ammon Post author

      Hi Josh,

      Glad it’s useful!

      I’m a bit stumped by this one. What shell (bash, tcsh, or whatever) are you using, and what version?

      That line just adds the table to the “tables” array, so the content of the table shouldn’t matter. I’m not sure why it’s acting like that is a command.

      Reply
    1. ammon Post author

      Some themes include links to the login page, rss feeds, and other links that you won’t want in the static pages. In order to keep those links out of the static pages, you’ll have to edit the theme files. Usually those consist of the footer.php and header.php and sidebar.php

      Reply
  7. Pingback: Static site hosting with S3

  8. Eric

    I’m using a WAMPStack, from bitnami.org for my local WP development. I want to grab everything and publish the static pages on a webserver (or S3). How can I execute the bash script on Windows? Attempting to connect to my local server via Putty does not work.

    Reply
    1. ammon Post author

      Unfortunately, this script will only work on a *nix system because it uses programs found there. The script would have to be rewritten for Windows.

      Reply
  9. Todd

    I posted an error I am receiving on the GitHub page, however, I will also add it here in case anyone else might be having the same issue.

    When I run the bash it reports back…

    ./wpstatic: line 121: conditional binary operator expected

    Any ideas?

    Reply
    1. ammon Post author

      Sorry for the long delay Todd. I will hopefully get a few minutes to take a look at it this week. I’ll post back here and at github. It most likely has something to do with differing BASH versions. In the mean time, you could try running it with some of the flags (skip some of the tests and such) might work.

      Reply
  10. Craig

    Hi Ammon,

    This seems exactly what I have been looking for I am just having trouble with the first step.

    1. I upload the script via ftp???

    2. How do I run the script?

    Thanks for the help!

    Reply
    1. ammon Post author

      Hi Craig,

      Glad this fits the bill. Thanks for posting the questions, I’m sure others have them as well.

      You’ll need to upload the script via ftp to the directory where your WordPress files are at (the same directory as the wp-config.php file).
      Then you’ll need to log into your server with a terminal/PuTTY connection and run the command on the command line.

      Most simply, that is just typing:

      ./wpstatic

      Also make sure you change the permissions on the file to at least by executable by your user (something like rwxrw-r–, which translates to 764).

      Reply
        1. ammon Post author

          Michael,

          You have to make sure you are in the same directory where the wpstatic file was uploaded. If you type in “ls -lh” and hit enter, and that will show you what files are in the directory. Type “pwd” and then enter to ‘print working directory’, that is, the directory path where you are at. You also have to make sure that the wpstatic file is executable by your user. Once you are in the same directory as the file, you can type “chmod u+x wpstatic” to allow your user to execute the file as a bash script. Hope that helps!

          Reply
    1. ammon Post author

      @Michael, Editing the theme just takes care of the login links, rss links, and stuff like that. You just need to take out the links or parts of the website that you don’t to have on the static version.

      Reply
    1. xdreamwalker

      Okay, so I changed my WordPress install to the root directory, then I ran ./wpstatic again and it created a wp-static file that seems to be the size of all of my content.

      The question now is what do I do with this large file? Is it an archive? Is is supposed to be a directory?

      I downloaded it and added a .zip to it thinking it might be archive, but it wouldn’t open.

      Reply
      1. ammon Post author

        It should be creating a folder with all of the content within. It is not archived (zipped). If I remember right, it needs to go in the folder where the wp-config.php file is, so that the paths to gather information are correct.

        Reply
    2. xdreamwalker

      Another thing that might be getting in the way of me getting this done is that after I hit y when it asks: Run wget now? I get a 403: forbidden error.

      Reply
      1. ammon Post author

        Do you have the site behind htaccess password protection? If so, the easiest thing to do is disable that while you make a static copy.

        Otherwise wget can take the user/pass info. When the wpstatic script prompts to run wget, say no (or n, or whatever). Then copy that command it says to run, but add in these two flags:

        –user=USERNAME –password=PASSWORD

        You’ll change USERNAME and PASSWORD to what they need to be, of course. So it would look something like this:

        wget –user=USERNAME –password=PASSWORD –mirror –cut-dirs=2 -P wp-static -nH -np -p -k -E http://example.com

        You would run that command directly on the command line, instead of with the wpstatic script.

        Hope that helps!

        Reply
  11. Moises Kirsch

    On MacOS X I’m getting this error when trying to run it:

    sed: illegal option — r
    usage: sed script [-Ealn] [-i extension] [file ...]
    sed [-Ealn] [-i extension] [-e script] … [-f script_file] … [file ...]

    Any ideas?

    Reply
    1. ammon Post author

      It looks like Mac OSX uses the -E flag instead of the -r flag. Just open the wpstatic file in a text editor and replace -r with -E .

      Reply
    1. ammon Post author

      The program should be able to convert any WordPress theme to static HTML pages. It just grabs what you see in a browser, plus the underlying CSS, JavaScript, and image files to make the page work.

      In any browser, you can view the source HTML (usually right click on a page and select “View Page Source” or something to that affect). What the program does, basically, is copy that information to an HTML file.

      Reply
  12. il katta

    at line 103

    ‘if [[ 0 > $home_cdirs ]]; then’

    don’t work, and don’t know why, but

    ‘if [[ "$home_cdirs" > 0 ]]; then’

    works!!

    Reply
  13. Marco

    Hello,

    I’ve this strange error
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

    What can it be?

    Thanks

    Reply
    1. ammon Post author

      Most likely you have something wrong with your MySQL connection or server. Maybe MySQL isn’t running. Only thing I can suggest is to do a google search and see what you come up with.

      Reply
      1. derry

        Gosh, I so would like to use this script locally on my mac. I was just about to call it quits when I tried something different.
        @ marco If you are running xampp like i am you should make a symlink in your root ‘private/tmp’ directory
        cd /private/tmp
        sudo ln -s /path/to/your/xampp/or/mamp/mysql.sockfile mysql.sock
        “my path was /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock” which i found in my my.cnf file in xampp

        and like stated in directions open terminal, and cd into the root directory where your wordpress is located and run “./wpstatic [optional flags]”

        This will get you pass this error message. However, I recieved this message later:
        Run wget now? [y/n]: y
        –16:07:30– http://localhost/wordpress/
        => `wordpress-static/index.html’
        Resolving localhost… 127.0.0.1
        Connecting to localhost[127.0.0.1]:80… connected.
        HTTP request sent, awaiting response… 404 Not Found
        16:07:30 ERROR 404: Not Found.

        FINISHED –16:07:30–
        Downloaded: 0 bytes in 0 files
        Converted 0 files in 0.00 seconds.

        I believe it has something to do with my local wordpress being installed in a sub directory and not the root of the htdocs.

        Any thoughts?

        Reply
  14. Kai

    Hi Ammon,

    great script, I think, but I can’t get it to work:
    wget: –cut-dirs: Invalid number `-1′.
    The script has been chmodded +x and is run with root-privileges in this case.
    The line-endings are unix, the shell is bash, run in an Ubuntu 12.04 x64 environment.
    The SELECT-Statement in line 92 gives a correct result, which ist resolvable.
    The error occurs on line 103, where
    home_cdirs=( $(echo $home_url | cut -d’/’ -f4- | awk -F/ ‘{print NF-1}’ ) )
    is done.
    I changed the cut -d’/’ -f4- to
    cut -d’/’ -f3-
    which makes the script work.
    I think this might be caused by the option_name = ‘home’ , which is set to http://mysite.com instead of http://www.mysite.com.

    Anyway, it works, and maybe someone might find my info useful.

    Kind regards and thanks

    Kai

    Reply
  15. Pingback: Permanently Convert WordPress Blogs to Static Pages – tumblilarius lukhnos | IT Lyderis

  16. john

    hi

    i do not have terminal access, as my site is hosted on a shared host. any way i can do this? or, can you make it a wp plugin?

    the output i need would be browsable on my local computer, not hosted on the web. So, no need to disable comments etc. Just need the absolute minimum to make the site browsable offline.

    thx!

    Reply
    1. ammon Post author

      You could also run this on your local computer, if that has terminal access. This would put it on your local computer already, and have it browseable from there.

      The bare minimum command you need to run is: wget --mirror -P blog-static -nH -np -p -k -E --cut-dirs=5 http: //sitename.com/blog/

      This should work with minor adjustments depending on what OS you run on the local computer and which flags or options that version of the wget command has.

      Otherwise, there are a couple of plugins out there already:
      http://wordpress.org/plugins/static-html-output-plugin/
      http://wordpress.org/plugins/really-static/

      Hope that helps!

      Reply

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="">