Tech

Thursday, April 10th, 2008

List pages in wordpress into a PHP Array

I spent some time figuring out how to get the wordpress wp_list_pages function to return the results as a PHP array of URLs and page names. There wasn’t an obvious way to do this and searching google did not yield anything particularly useful. The wp_list_pages function has an “echo” parameter which when set to 0 returns a list of links as a string. That won’t help much if you want it as an array with links and page names.

I ended up writing a short script that will query wordpress for the name and URL of all of the subpages of a specified page. This could be used to make a function that formatted a list of pages a particular way (like making the list wrap into two or more columns). Its nothing fancy, but perhaps it can help other wordpressers who are looking for something similar.

<?php
$querystr = “SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘page’ AND $wpdb->posts.post_parent = [[Parent Page ID]] ORDER BY $wpdb->posts.post_title ASC”;

$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
// Add your own logic or formatting here
?>
<a href=”<?php the_permalink() ?>” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a>
<?php endforeach;
else :
echo ‘Not Found’;
endif;
?>

Where [[Parent Page ID]] is the id number of the parent page you would like to get children from. If you want all pages included, don’t include this parameter.

Thursday, December 20th, 2007

Buxfer - customer service that can read your mind

I use buxfer to keep track of expenses among friends, it had really streamlined paying for trips and meals and cut down on the confusion. I use it almost daily.

Recently, I attempted to add a new group to share expenses among friends but I kept getting a popup that said I had encountered a bug with a form to submit a report. As I was in a hurry, I didn’t submit a report but almost instantly I got an email from Buxfer:

Hi Brendan,

We are having some difficulties with the group creation
functionality. It will be resolved by the end of the day
today. Please be patient until then.

Thanks so much!

That is the kind of support and customer service more web apps should have. I didn’t even submit a bug report.

Thursday, October 25th, 2007

Automatic wordpress backups to Amazon S3

I use wordpress for many sites, and its really important for me to be able to automatically perform nightly backups of not just the database, but all files including uploads, themes, plugins and the basic wordpress files. I need the ability to revert any file back to a previous version.

I found a variety of solutions to backing up files using S3, but none that exactly met my needs, so I decided to post my work here.

Note that these instructions assume your web server is Unix, Linux or Mac OS X based. It also assumes you have a basic understanding of FTP, SSH, firefox extensions and compressed files.

1. Get the s3-bash code library

The s3 BASH library is a collection of scripts that can get, put or delete files from Amazon s3. You can grab them from Google Code.

2. Unpack and upload these scripts to your server.
They should probably live outside of your webroot. Depending on how your server is set up you might put them in:

/home/USER/s3

Where USER is your username.

3. Sign up for an Amazon S3 Account
You’ll need an Amazon S3 account, so sign up and then get your access key ID and secret access key on the AWS access identifiers page.

4. Put your secret access key in a text file and upload it to the server
You could call it s3.key and put it in the same folder you uploaded the s3-bash scripts to. Chmod it to 640.

chmod 640 /home/USER/s3/s3.key

5. Create a folder for the backups
Make a folder on your webserver for backups, perhaps:

/home/USER/backups

Where USER is your username.

6. Create a bucket on Amazon S3 for your backups
The easiest way I found to do this is to use the firefox extension S3 Firefox Organizer. Add the extension, restart firefox and then in Firefox go to “Tools” > “S3 Organizer”. Click “Manage Accounts” and enter your access key ID and secret access key and then login. Create a bucket (directory) for your backups. I created a one called bnee.

7. Write the backup script
This is the tricky part. Here is the script I came up with. The [[Double Bracketed]] items are places where you’ll have to enter information specific to your account (see below).

#!/bin/bash
USER=[[USER]]
mysqldump –add-drop-table -u [[DBUSER]] -p[[DBPASSWORD]] -A >/home/$USER/backups/mysql.sql
rm /home/$USER/backups/backup.tgz
tar -czvf /home/$USER/backups/backup.tgz [[PATHTOWEBROOT]] /home/$USER/backups/mysql.sql
NOW=$(date +_%b_%d_%y)
/home/$USER/s3/s3-put -k [[S3ACCESSKEYID]] -s “/home/$USER/s3/s3.key” -T /home/$USER/backups/backup.tgz -v /[[S3BUCKET]]/backup$NOW.tgz

[[USER]] - Because the username of my web server is used a lot in the paths, this is a variable
[[DBUSER]] - The username of the mysql database. You can get this from the wp-config.php file of your wordpress installation.
[[DBPASSWORD]] - The password for your mysql database. You can get this from the wp-config.php file of your wordpress installation.
[[PATHTOWEBROOT]] - The path to the webroot. Its likely something like /home/USER/public_html
[[S3ACCESSKEYID]] - The access key ID for Amazon S3 (see step 3).
[[S3BUCKET]] - The name of the bucket (directory) you created in step 6.

You might have to change the various paths depending on the way your web server is set up and the folder names you chose in previous steps.

Paste all of this into a text file and name it something like backup.sh. Upload it to your web server, outside of the webroot.

/home/USER/backup.sh

8. Test the script
Verify that the script is working by running it.

sh /home/USER/backup.sh

If everything is working ok, you’ll see a lot of output as each file of your website is added to the compressed backup and then uploaded to Amazon S3. It may take a minute or so to run, but when it is done you can verify that the script worked by using the S3 Firefox plugin to check that the file ended up where you expected it to.

9. Automate it
Add a cron job to run it every night

0 0 * * * sh /home/USER/backup.sh

If everything is configured correctly, this should perform a full backup nightly at midnight.

Tuesday, October 16th, 2007

My first music purchase since 2000

Today I downloaded the latest Radiohead album, In Rainbows. Unlike all the other albums I’ve obtained since I stopped purchasing compact discs, I purchased this album directly from the artists on their website and downloaded the album directly in just a few minutes rather than from peer-to-peer networks.

I chose to pay £5.00, which gives Radiohead much more income on that album than if it was sold via a record label in stores, and it gives me direct non-DRM access to the mp3s I want. What I don’t want is a physical CD. I probably wouldn’t pay £5.00 for most albums, but since Radiohead is pioneering a different model I thought I would give them some financial encouragement.

The last mainstream album I purchased was Blue Man Group which I bought after seeing them perform on a high school band trip. I purchased the disc used on half.com, so maybe that doesn’t even count as a real record sale. More recently I bought all of the albums in compact disc form from the New Orleans Bingo! Show off of the Louisiana Music Factory Website. (If Clint or any of the Bingo Show members read this, I love your music and would really love to be able to download it from y’all directly using the Radiohead model, and if anyone from the Lousiana Music Factory reads this, I like lots of your musical offerings but your website really needs some user interface work and search engine optimization).

Update: In writing this post, I realized that the first New Orleans Bingo! Show CD, Bingo!, is now available to order so I ordered it for $12.99 plus $5 shipping. I’m curious what percentage the band receives from that sale.

Sunday, October 7th, 2007

Buxfer, Mint & Quicken

The future of personal finance software will be online. I regularly use buxfer to keep track of shared expenses with my roommates and friends. Often we’ll share groceries or meals and its easy for one person to pay the whole bill, then enter the transaction online later and split it evenly (or unevenly) among the participants. This is a lot less messy than everyone getting out their wallets to see who can make change then everyone accounting for their share of tax & tip. Its a bit fairer than alternating payers, as it doesn’t matter if you go to a spendy restaurant one night and a cheap one the next.

Buxfer recently added the ability to track your shared payments by account and to import transactions from a very limited set of banks. This expands buxfer from just a shared payment application to a personal finance tracking application. This space is filling rapidly. Mint.com recently won the techcrunch 40 competing against 39 other tech startups. Wesabe and Geezeo are similar applications, although mint.com has the cleanest interface.

I’ve used quicken to track my finances since starting college (and have the graph showing the inverse relationship between net worth and education to prove it). I am still using it but I’m ready to jump ship once there is a viable online application that can download info for all of my accounts and track stocks & loans. Right now, mint and wesabe do not provide the loan & stock tracking I’d like but most importantly, neither integrates properly with my checking account at Mechanics Bank.

The last thing I want is what they currently offer on the front page of their site: a box of software. I can’t remotely manage my finances using quicken nor can I use any computer except for the one on which it is installed. I also get the responsibility of dealing with backups and updates.

It seems to me that if Intuit wanted to stay relevant, they would either create a competitor to these finance startups or acquire one. They certainly have the widespread brand recognition to attract non-early-adopter users to the service and also already have established relationships with most financial institutions in terms of syncing transactions. Given the state of quickbooks online which requires Windows AND Internet Explorer, I’d imagine that an aquisition might be the more effective route.

Geezeo added the ability to track investments and loans this week, so I’ll have to check that out. I love the concept behind buxfer and it would be great to be able to use one application to track my personal finances and shared expenses. They have a ways to go until they’ve got something to compete with mint, but at the rate they have been adding features it may not be long.

Its only a matter of time until I can reduce the commercial software count on my computer.

Wednesday, October 3rd, 2007

Facebook Scores - Mashup with google maps, facebook data & census data

I looked at the facebook network for La Crosse, WI, my hometown and was impressed that there are 12,856 people on this network alone. People on facebook can belong to only one regional network at a time, so its likely that most of these users actually live in the La Crosse metro region. The city of La Crosse has a population of 51,818 and the metro region has a population of 126,838.

facebook_lse.jpg

Surrounding cities, like Onalaska, WI don’t have their own facebook network, so people living in these cities are no doubt in the La Crosse network. Regardless, this means that 10% of the population of Metro La Crosse is on facebook.

La Crosse is not extemely tech savvy, but 10% is much higher than the New Orleans, LA facebook score. With 42,789 facebook users in the New Orleans network out of an estimated 1.2 million metro New Orleans population, New Orleans facebook score is a 3.8%.

San Francisco (city, not metro area) scores 15.4% (114,792 users of 744,041 population) and New York is 5.1% facebooked (425,963 users with 8,214,426 population).

It would be an interesting mashup to take facebook network membership numbers, us census data and put them on google maps.

Friday, September 28th, 2007

The true shape of the Midwest- Google Analytics

Maybe for some people who don’t know the midwest as well as I do the google analytics visitor overlay map looks fine. However, the shape of my home state, Wisconsin, is certainly distorted and Michigan is almost unrecognizable. This overlay uses the states boundaries that extend over water. For most states that does not affect the overall shape very much but for the upper midwest it makes a big difference.

For reference, heres a google map of the same area.