WordPress get_template_directory() working on localhost and dev server with same database

I like to develop locally on localhost, then push everything to a testing server. Both localhost and my testing server are using the same database. The only problem is get_template_directory() returns my testing server path.

Can I use .htacess or something else to return the corresponding local/live path from get_template_directory()?

Related posts

Leave a Reply

2 comments

  1. I use a variation of a method suggested by Mark Jaquith in his WordPress Skeleton setup .

    The idea of this is that by adding the following to the top of your wp-config.php file:

    if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) {
    
        include( dirname( __FILE__ ) . '/local-config.php' );
    
        define( 'WP_LOCAL_DEV', true );
    
        return;
    }
    

    you can create a local-config.php file that can be used to specify separate settings for local development. As long as you don’t add this file into your source repository then it won’t be present when you deploy to the staging/production servers and the settings in the standard wp-config.php file will be used instead.

    You could then filter theme_root depending on the state of the WP_LOCAL_DEV constant, or specify different constants in the local config file itself, e.g.

    define('WP_CONTENT_DIR', realpath($_SERVER['DOCUMENT_ROOT']) . '/wp-content');
    define('WP_CONTENT_URL', '/wp-content');
    define('WP_PLUGIN_DIR', realpath($_SERVER['DOCUMENT_ROOT']) . '/wp-content/plugins');
    define('WP_PLUGIN_URL', '/wp-content/plugins');
    

    Hope that helps.

  2. I found that you can define the site in wp-config.

    I added the following to wp-config.php:

    define('WP_HOME','http://localhost/wp_root');
    define('WP_SITEURL','http://localhost/wp_root');
    

    Now all links and images are served with correct paths.