1

My site URL in the database shows sitea.com. Is it possible to change this via functions.php into siteb.com, however still maintaining sitea.com in the database?

My current scenario is that I have three developers who work locally and we want to use one database. We all connect to a remote database, however, the URLs from our local development environment and from the remote database are different which causes broken links.

Is there some way to change the siteurl in functions.php while still having the same URL in the database?

Thanks in advance.

  • I've been able to change it now, however get_template_directory still uses sitea.com is there a way to change it globally so other functions are affected or to I need to individually hook modifications into those? – iamTony Jan 19 '17 at 7:47
  • This sound like you don't use the wordpress api to get urls everywhere, or your development includes cpntent which it should just not do. Themes and plugins should be url independent. – Mark Kaplun Jan 19 '17 at 7:53
  • @MarkKaplun Can you elaborate more there seems to be a miscommunication. – iamTony Jan 19 '17 at 7:55
  • Not sure on what ;) themes and plugins should not care on what doimain they are active. If they are domain specific than they are not written correctly – Mark Kaplun Jan 19 '17 at 7:56
  • get_template_directory_uri() returns an absolute path, from my db it's siteA. what I need to do is modify this and every other uri functions to show siteB. But still maintaining Site A in the database – iamTony Jan 19 '17 at 7:58
3

If instead of modifying functions.php you can modify wp-config.php, you can use the following:

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

Source: https://codex.wordpress.org/Changing_The_Site_URL#Edit_wp-config.php

| improve this answer | |
  • This is the correct answer. If the question is "How to change site URL from a theme?" that answer is DON'T. If the question is "How to change the site URL with code?" Then @fergbrain nailed it. But the original question actually is about local development, so the answer is still slightly different. – Matt Cromwell Jan 21 '17 at 22:23
  • +1 I thought that defining WP_HOME and WP_SITEURL in wp-config.php updated the DB, which the OP specifically does not want, but after double checking, I confirmed the options in the DB are not affected. – Dave Romsey Jan 21 '17 at 22:46
2

This shouldn't be handled by your theme at all, but your local wp-config.php file. You can conditionally load local and unique wp-config definitions by wrapping in a conditional file_exists. Here's an example from Mark Jaquith:

if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) {
    include( dirname( __FILE__ ) . '/local-config.php' );
    define( 'WP_LOCAL_DEV', true ); // We'll talk about this later
} else {
    define( 'DB_NAME',     'production_db'       );
    define( 'DB_USER',     'production_user'     );
    define( 'DB_PASSWORD', 'production_password' );
    define( 'DB_HOST',     'production_db_host'  );
}

See here for more details: https://markjaquith.wordpress.com/2011/06/24/wordpress-local-dev-tips/

| improve this answer | |
1

The theme_root_uri filter will allow the URLs returned by get_stylesheet_directory_uri() and get_template_directory_uri() to be altered on the fly:

/**
 * Filters the URI for themes directory.
 *
 * @since 1.5.0
 *
 * @param string $theme_root_uri         The URI for themes directory.
 * @param string $siteurl                WordPress web address which is set in General Options.
 * @param string $stylesheet_or_template Stylesheet or template name of the theme.
 */
function wpse_theme_root_uri( $theme_root_uri, $siteurl, $stylesheet_or_template ) {
    // $siteurl will be http://sitea.com via get_option( 'siteurl' )
    return str_replace( $siteurl, 'http://siteb.com', $theme_root_uri );
}
add_filter( 'theme_root_uri', 'wpse_theme_root_uri', 10, 3 );

The plugins_url filter will allow the URL returned by plugin_dir_url() to be altered in a similar fashion:

/**
 * Filters the URL to the plugins directory.
 *
 * @since 2.8.0
 *
 * @param string $url    The complete URL to the plugins directory including scheme and path.
 * @param string $path   Path relative to the URL to the plugins directory. Blank string
 *                       if no path is specified.
 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
 *                       is specified.
 */
function wpse_plugins_url( $url, $path, $plugin ) {
    return str_replace( get_option( 'siteurl' ), 'http://siteb.com', $url );
}
add_filter( 'plugins_url', 'wpse_plugins_url', 10, 3 );

Looking through /wp-includes/link-template.php, there are quite a few other functions that return URLs, but they can be filtered using the same approach as demonstrated above.

The values for the options siteurl and home can be modified on the fly as well:

function wpse_pre_option_siteurl_and_home( $pre_option, $option ) {
    return 'http://siteb.com';
}
add_filter( 'pre_option_siteurl', 'wpse_pre_option_siteurl_and_home', 10, 2 );
add_filter( 'pre_option_home',    'wpse_pre_option_siteurl_and_home', 10, 2 );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.