Using WordPress as a Webcomic Archive

Now the fun part. Say you have 300 some comics already drawn. Typically, what you would have to do is make a blog post for each and every comic, with an IMG tag linking to each file on your server. Not a fun way to spend an afternoon. Fortunately for you, some hack programmer already came up with a way to automate the process!

If you’re starting a comic from scratch, or just have five or six comics to your name and don’t mind writing a post for each of them, feel free to skip this section.

To put all your comics in the right place, we’ll write a small PHP file that will look at a comic’s file name, determine it’s place in the archive, and put an IMG tag into a blog post.

The way Keenspace formatted comics was based upon the filename. A comic for October 31st, 2005 would have to be titled 20051031.gif, or 20051031.jpg, or 20051031_thefirsteightdigitsareallthatreallymatters.png. We’ll use this naming method for this tutorial, but feel free to edit this to your needs.

We’re basing our PHP file on the same code used to publish a post in WordPress. When I upload a comic through FTP, it’s in a folder called “update” and has this updater script in it (you can call it updateme.php, and just call it from your browser once the comic is uploaded.) This reads the first 8 letters of the comic filename (the 20051031 date thing,) makes a WordPress post for that date at 7AM, stores the URL to that comic and moves the comic file into the right folder on my server. There’s also a safeguard in here so it only checks files with .gif, .jpg, or .png file extensions.

$dbcnx = mysql_connect("DBURL", "USERNAME", "PASSWORD");
$path = "./";
$path = "./"; $dir_handle = opendir($path) or die("Unable to open directory $path"); while ($file = readdir($dir_handle)) { $filetyp = substr($file, -3); if (($filetyp == 'gif') OR ($filetyp == 'jpg') OR ($filetyp == 'png')) { $keendate = substr($file, 0, 8); $urlstart = ""; $newfile=$file; settype($newfile, "string"); $urlcode = $urlstart . $newfile . $urlend; /* $sql = "INSERT INTO comicsdb (comic_date, comic_url) values ('$keendate', '$urlcode')"; //Barf the info back into the SQL table $result=mysql_query($sql); if (!$result) { die('Invalid query: ' . mysql_error()); } */ //Fake a WordPress Entry for the comic $post_title = $keendate; $content = $urlcode; $timecode_year= substr($file, 0, 4); $timecode_month= substr($file, 4, 2); $timecode_day= substr($file, 6, 2); //fake a timecode, eg: 2000-06-30 00:35:27 $timecode_time = "07:00:00"; $timecode_time_GMT = "11:00:00"; $post_date = $timecode_year . "-" . $timecode_month . "-" . $timecode_day . " " . $timecode_time; $post_date_gmt = $timecode_year . "-" . $timecode_month . "-" . $timecode_day . " " . $timecode_time_UTC; $sql2 = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_category, post_status, comment_status, ping_status) VALUES ('1', '$post_date', '$post_date_gmt', '$content', '$post_title', '$post_category', 'publish', 'closed', 'closed')"; $result = mysql_query($sql2); if (!$result) { die('Invalid query: ' . mysql_error()); } //Move the file back to the archive rename($file, "../comics/".$file); echo 'The following file has been moved: '; echo $file; echo '
'; } }

I threw my whole archives into the “update” folder on my FTP account, called updateme.php, and it looped through all the comics, putting them in the right place and posting them correctly into the blog. Granted, this code is a bit sloppy, and the post times get a little glitched (you can manually edit the comic’s post date through the WordPress Dashboard… no worries!) But it gets the job done. Anyone who can find a way to fix this, be sure to e-mail me!

Now, you have options here. Once the archives are full, you could use this script for all your future site updates. Just FTP the files to the update folder, run the script, and you’re set. Even works for uploading comics to future dates, just be sure to name your comics in the 20051031 format. BUT, there’s an even easier way to upload your brand-new comics, right through the WordPress interface!

Pages: 1 2 3 4 5