Fixing Wordpress URLs When Using Rack

27 Mar 2014

In my last article I detailed how to run PHP through a Rack server. This works fairly well until you try to sign into the Wordpress admin section.

The problem is that Wordpress stores site URLs in the database and it will use these for some redirections. Luckily, with a few Rake tasks you can painlessly override them.

I borrowed some conventions from Rails and created a config/database.yml file. This is your run of the mill database connection file, nothing special here.

From there I created a Rakefile in the root of the project titled rakefile.rb.

I've been spoiled by Rails' database Rake tasks so I decided to build my own using ActiveRecord.

Here are the Rake task components that setup the ActiveRecord connections:

require 'yaml'
require 'logger'
require 'active_record'

namespace :db do
    def create_database(config)
        options = { :charset => 'utf8', :collation => 'utf8_unicode_ci'}

        create_db = lambda do |config|
            ActiveRecord::Base.establish_connection config.merge('database' => nil)
            ActiveRecord::Base.connection.create_database config['database'], options
            ActiveRecord::Base.establish_connection config

        rescue ActiveRecord::StatementInvalid => exception
            puts "There was an error creating the database. The error is: #{exception.message}" and return

        puts "Database #{@config['database']} was created."

    task :environment do
        DATABASE_ENV = ENV['DATABASE_ENV'] || 'development'

    task :configure_connection => :configuration do
        ActiveRecord::Base.establish_connection @config
        ActiveRecord::Base.logger = STDOUT if @config['logger']

Now that we have that out of the way, we can create our database create and drop table tasks. I put these in the db namespace.

desc 'Create the database from config/database.yml'
task :create => :configure_connection do
    create_database @config

desc 'Drops the database'
task :drop => :configure_connection do
        ActiveRecord::Base.connection.drop_database @config['database']
    rescue MySql2::Error => exception
        puts "There was an error dropping the database. The error is: #{exception.message}"
        puts "Database #{@config['database']} was dropped."

Now we can create and drop our database with the familiar commands of rake db:create and rake db:drop.

There is only one more thing to do and that's fix the Wordpress site URL references. I added the following task to the db namespace:

desc "Set Wordpress URL's to localhost"
task :fix_wordpress_urls => :configure_connection do
            "UPDATE wp_options SET option_value = 'http://localhost' WHERE option_name IN ('siteurl', 'home');"
    rescue ActiveRecord::StatementInvalid => exception
        puts "There was an error fixing the Wordpress URLs. The error is: #{exception.message}"
        puts "Wordpress URLs have been fixed."

With that final task we can now run this command:

rake db:fix_wordpress_urls

Now our Wordpress instance is ready to be run through Rack.

Final Thoughts

This is how I thought to manage this particular PHP/Wordpress hurdle in a Rails dominated environment. Hopefully some of you can apply this, and let us know if you have any other clever solutions.

Brief mention in the Portland Business Journal

26 Mar 2014

Wordpress and PHP on Rack

26 Mar 2014

We work primarily in Ruby on Rails, but every once and a while a client will need us to fix a critical bug in an existing PHP/Wordpress app that is slated to be deprecated.

We use Pow on our development machines. This is great for developing Rails applications but it does't play so well with other stacks that require port 80 to run.

There are some instructions out on the web that show you how to use Apache in conjunction with Pow so that both apps can be served simultaneously. This seemed like a little too much overhead, so I started poking around for a solution.

Did you know that PHP comes with an embedded web server? Yup, it's built in as of PHP 5.4 and OS X Mavericks comes with it pre-installed. If you're looking for a newer version, you can install 5.5 using homebrew-php.

Now that we don't need Apache to run PHP, we'll need something to proxy requests back to our embedded server. For this, I used Rack.

I setup a simple Gemfile with the following gems:

gem 'rack'
gem 'rack-legacy'

Then it's time to create our rackup file. Create a file titled and add the following:

require 'rubygems'
require 'bundler'


require 'rack'
require 'rack-legacy'

use Rack::ShowExceptions
use Rack::Legacy::Index
use Rack::Legacy::Php
run Dir.getwd

That's it! Place this file into the root of your PHP project and you should be able to call rackup from the terminal to start the Rack server. By default this will be at

There is one caveat though. At this time, Rack Legacy, will only respond to requests if it thinks that it's a valid PHP file.

From the Rack Legacy library this is called before a request is proxied:

def valid? path
    return false unless path =~ /\.php/

    path = path[1..-1] if path =~ /^\//
    path = path.split('.php', 2)[0] + '.php'
    path = ::File.expand_path path, @public_dir
    ::File.file? path

In our case this wasn't working for us because of the way that pretty URLs were being handled by Wordpress. In order to get around that, I monkey-patched Rack Legacy to simply pass all requests back to PHP.

Here is what I added to the file to make this happen:

class Rack::Legacy::Php
    def valid? path
        return true

With that change, the Wordpress site was working as expected.

How to: Delete a remote git tag

25 Mar 2014

The Weekly Orbit - Week of March 17th

24 Mar 2014

Topics discussed in the chat room this week

Continue reading …

Airbnb discusses their thoughts on the future of web apps.

20 Mar 2014

The Weekly Orbit - What We're Talking About

14 Mar 2014

Topics discussed in the chat room this week

Continue reading …

Olivia's thoughts on the March PDX DIGITAL PM MEETUP

13 Mar 2014

A/B Testing at Shutterstck

12 Mar 2014

Great experiences and your brand

6 Mar 2014

What makes people passionate, pure and simple, is great experiences. If they have great experience with your product and they have great experiences with your service, they’re going to be passionate about your brand, they’re going to be committed to it. That’s how you build that kind of commitment.

Jesse James Garrett from What the Heck is User Experience Design??!! (And Why Should I Care?)

Leading from the base camp

5 Mar 2014

Once leadership was all about planting your flag on the summit and standing heroically for a photograph. Now the leader is the one pacing anxiously at base camp waiting to hear good news.

Rob Goffee, Gareth Jones from Clever: Leading Your Smartest, Most Creative People

Really enjoying this book, which was recommended to me by Dan Saffer a while back.

CKEditor and Word Counts

4 Mar 2014

In a project we’re currently working on, we created a form builder that could publish complex forms that include features such as:

  • Logic that can be applied to different fields, sections and pages
  • Configurable validations
  • Word counts by field, section or complete form
  • Option to allow some textareas to have WYSIWYG content

Continue reading …

It's the small things...HipChat's HEX swatches

4 Mar 2014

Several months ago, our team left Campfire and entered HipChat for all internal conversations project, team, or cute kitten related. There are a few great things about using HipChat, but one of my favorite’s as a designer is this; adding a swatch of any HEX code. This feature not only provides my teammate with the code to add for a color, but also provides more context when we are collaborating. It’s a small thing, but sometimes, the small things just make life a little better.

What are the small things you like about the tools you use?

Continue reading …

Robby Russell is not just the chief evangelist at Planet Argon...

3 Mar 2014

Don't Push Me (yet)

3 Mar 2014

The Weekly Orbit

28 Feb 2014

Topics discussed in the chat room this week

Continue reading …

Fresh URL - Rid the internet the scourge of the tracking code

27 Feb 2014

Lessons from Planet Argon Junior Developers

27 Feb 2014

Planet Argon hired two Epicodus graduates as junior Ruby on Rails developers: Abby Ihrig and Ana Tighe. I took some time to sit down with them and see how things are going so far. It was great talking to them about their experiences starting as junior developers here. We touched on some interesting points, including company culture, favorite apps, music, and some insights for people interested in development.

Abby Ihrig and Ana Tighe

Continue reading …

Future Talk with Sandi Metz

27 Feb 2014

Sandi Metz explained her 5 rules (and why they matter) at Future Talk at New Relic on Monday the 24th.

Continue reading …

Javelin security product hits the market

24 Feb 2014

Easy search and filtering in Rails

21 Feb 2014

Justin Weiss posted a great article on how you can add quick and easy filtering for Rails models without bloating your controllers or models beyond adding scopes here

Continue reading …

Jason Fried on The Talk Show

21 Feb 2014

Sourcegraph ditches AngularJS

18 Feb 2014

Tips for prospective Juniors

13 Feb 2014

Entering the field as a Junior Developer can be intimidating. Here are a couple of experiences I found important in my preparation to making the transition into full time development.

Continue reading …

On App Notifications

4 Feb 2014

This is why facebook stopped emailing copies of notifications to people’s inboxes by default: they learned that emailing people less about what’s happening in the app actually keeps them coming back more.

holympus from Notifications are a UX Anti-Pattern