Archive for the ‘WordPress’ Category

WordPress Blog and Multiple Server Names

Friday, February 26th, 2010

I was recently updating my WordPress installation to a freshly released version. My traditional procedure of updating the website is to first make changes to a test version (which is set up on a subdomain of odyniec.net), make sure everything works and looks right, then copy the test files to the public location.

Unfortunately, WordPress didn’t get along with this solution especially well. The WordPress engine didn’t like being accessed through the test subdomain, as it expected a single host name to be designated as its home location. Even though I could open the blog at the test address, all the dynamically generated links to scripts, style sheets, archives and such still pointed to the live version. I also couldn’t log in to the administrative interface at the test version, as it kept redirecting me to the live login page.

I knew PHP stored the actual host name that was used to access the page in the $_SERVER['SERVER_NAME'] variable, and I thought a simple solution would be to make WordPress use that instead of the basic URL set in the configuration. I had very little experience with WordPress internals, so it took me some time to figure out how to implement this, but I eventually succeeded. I wrapped the code into a plugin so that the functionality can be easily enabled or disabled and doesn’t affect the base WordPress code.

So, behold! My first WordPress plugin ever:

<?php

/*
Plugin Name:    Server Name Support
Description:    Allows the blog to be accessed with different server names
Author:         Michal Wojciechowski
Version:        0.0.1
Author URI:     http://odyniec.net/
*/

$sns_home = preg_replace('!://[a-z0-9.-]*!', '://' . $_SERVER['SERVER_NAME'],
    get_option('home'));

function sns_home()
{
    global $sns_home;

    return $sns_home;
}

function sns_replace_host($url, $path = '')
{
    return preg_replace('!://[a-z0-9.-]*!', '://' . $_SERVER['SERVER_NAME'],
        $url);
}

add_filter('pre_option_home', 'sns_home');
add_filter('pre_option_siteurl', 'sns_home');
add_filter('pre_option_url', 'sns_home');
add_filter('stylesheet_uri', 'sns_replace_host');

add_filter('admin_url', 'sns_replace_host');

?>

In case you’d like to use it (not very likely, as I don’t think my use case is that common), all you need to do is save the above code in a .php file, place it in the wp-content/plugins directory, and activate it in the WordPress admin interface.

  • Archives

  • Categories

  • Meta

  • Latest Tweets


    Warning: Illegal string offset 'last_access' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 334

    Warning: Illegal string offset 'time_limit' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 334

    Warning: Illegal string offset 'last_access' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 336

    Warning: Illegal string offset 'twitter_api' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 234

    Warning: Illegal string offset 'user_token' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 262

    Warning: Illegal string offset 'user_secret' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 263

    Warning: Illegal string offset 'consumer_key' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 264

    Warning: Illegal string offset 'consumer_secret' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 265

    Warning: Illegal string offset 'twitter_username' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 270

    Warning: Illegal string offset 'show_retweets' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 272

    Warning: Illegal string offset 'exclude_replies' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 275

    Warning: Illegal string offset 'twitter_data' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 282

    Warning: Illegal string offset 'twitter_data' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 350

    Warning: Illegal string offset 'twitter_data' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 351
    Twitter outputted an error:
    .
    Warning: Illegal string offset 'time_format' in /usr/local/www/odyniec.net/public/blog/wp-content/plugins/twitget/twitget.php on line 484
  • Follow odyniec on Twitter