Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I set SITE_URL to override the database url info? WordPress

  • SOLVED

So I am at a company and I was asked to set up a new blog. First, I went to:

dev.site2.mysite.com

Which is part of the dev site. I installed the blog there, activated, and imported some content from an older blog.

Then I committed it to git.

Then I realized I had committed it to the wrong folder, which also maps to the wrong sub-domain. So I move it to:

dev.public.mysite.com

Then I commit this to git, merge to the master branch, and we push it out to our staging server, which is at:

admin.mysite.com

I look at the site and the CSS and JS are broken, because they seem to be hardcoded to:

dev.site2.mysite.com

I thought I could override this in the wp-config.php file so I open it and do something like:

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

which did not work, even when pushed out. It seemed to have no effect -- the style sheets are still linking to dev.site2.mysite.com.

Also, I need these variables to match the many sub-domains we have (we have a complicated and multi-stage development and staging and testing platforms).

So I fetched some info from $_SERVER and made this a variable:

define('WP_HOME', $server_base_uri . '/blog/');
define('WP_SITEURL', $server_base_uri . '/blog/');

But still, I commit this to the master branch in git, and we deploy to staging, but still, on staging, the style sheets and Javascript are hard-coded to dev.site2.mysite.com.

I could probably muck around in the database and tear out any incidence of dev.site2.mysite.com, but I am curious if there is a way to do this so that these URLs automatically adjust to the many sub-domains we have?

UPDATED:

This is crazy. I got into this part of the dashboard:

/wp-admin/options-general.php?settings-updated=true

and I managed to reset WordPress Address (URL) and SITE URL, and yet when I reload the page, it still has the style sheets and javascript links hard-coded to dev.site2.mysite.com.

And no, I have not yet installed any kind of cache plugin.


UPDATED:

Check out the screenshot that I am uploading. I can log into the dashboard and make some edits from there, but I don't get the header or footer.

Answers (5)

2012-09-28

Albert Shala answers:

Have you tried updating it via your functions.php ?
update_option('siteurl','http://example.com/blog');
update_option('home','http://example.com/blog');


Lawrence Krubner comments:

No, I have not tried that yet, but is that not the same as:


define('WP_HOME', $server_base_uri . '/blog/');
define('WP_SITEURL', $server_base_uri . '/blog/');

What is the difference?

2012-09-28

Francisco Javier Carazo Gil answers:

Hi Lawrence,

The difference between this:
define('WP_HOME', $server_base_uri . '/blog/');
define('WP_SITEURL', $server_base_uri . '/blog/');

And this:
update_option('siteurl','http://example.com/blog');
update_option('home','http://example.com/blog');

Is the next. The first one defines a preprocessor directive that will override value in database but the second one change this value (the value is placed in wp_options).


Francisco Javier Carazo Gil comments:

What would I do to solve your problem?

0) Do define and update_options
1) Find the bad URL in all your files of your theme and if you find it replace it
2) Update the bad value in your database. Some values are serialized, you won't be able to execute a standard SQL query, you will have to use this one: http://interconnectit.com/124/search-and-replace-for-wordpress-databases/ (the perfect tool :))
3) Switch off any cache system


Lawrence Krubner comments:

So if I do this then I automate the updating of the values in the database? That would be fantastic. So why does anyone ever do:

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

Why not always use update_option ?


Francisco Javier Carazo Gil comments:

Well, use define is faster than update_option.

If you use define, WordPress won't make any SELECT query against database, so it is faster, but at the same time, the best option is make the two actions: update in DB and define.

2012-09-30

Spencer Tomosvary answers:

This sounds like it is clearly an issue with your database and serialized data that can be resolved a few different ways...

With some clever mysql commands..
this probably won't work and you've probably tried it already but very carefully and painstakingly replacing urls on your .sql file (from the phpmyadmin export) in a text editor...

and now I'm getting closer to an answer for you...

I don't know how many database backups you have, but hopefully you have a backup of it from when everything was last working correctly, before migrating to a new domain.

Each time you've changed the domain, updated the urls from the wp-admin dashboard or functions.php or phpmyadmin or text editor, your're adding complications to it.. you want to back track as far as possible so you have as few as possible variations in the urls you need to modify..

here is another possibility worth mentioning...

Load the site and DB back up on the last url it was where it worked.. do an export of the contents, theme options and widgets (there is a nice plugin out there to download widget data in a json file - also some other plugins I've seen to download all your plugin settings and serialized data in a json file)

Then install a new instance of wordpress and import all the data..

Okay finally here the possible best solution for you... Using the oldest .sql backup you have (to avoid all the URL changes and modifications you made in trying to solve the problem which are just making it worse) go to this url:
[[LINK href="http://interconnectit.com/124/search-and-replace-for-wordpress-databases/"]][[/LINK]]

http://interconnectit.com/124/search-and-replace-for-wordpress-databases/

Download and install the tool on your server (very easy) and run it.. This can swap out/replace your urls properly unlike a text editor and should hopefully fix the problem for you. If for some reason this doesn't work, I'd go back and try the second to last option I mentioned.

:)



Lawrence Krubner comments:

This looks like a very good tool:

[[LINK href="http://interconnectit.com/124/search-and-replace-for-wordpress-databases/"]]http://interconnectit.com/124/search-and-replace-for-wordpress-databases/[[/LINK]]

Thanks for the tip.

2012-09-28

Abdelhadi Touil answers:

Hi.
I'm not sure if I uderstand well your problem, do you mean that you'v installed first the blog on dev.site2.mysite.com, the you'v moved it to dev.public.mysite.com and finally to admin.mysite.com, and the problem is that style sheets and javascript links are hard-coded to dev.site2.mysite.com? Please confirm.
By the way, what about .htaccess file? maybe problem comes from there. Just a guess.
Good luck.


Lawrence Krubner comments:

Yes, you summarize this correctly:

<blockquote>do you mean that you'v installed first the blog on dev.site2.mysite.com, the you'v moved it to dev.public.mysite.com and finally to admin.mysite.com, and the problem is that style sheets and javascript links are hard-coded to dev.site2.mysite.com? </blockquote>

and then in the config file I do:

define('WP_HOME', $server_base_uri . '/blog/');
define('WP_SITEURL', $server_base_uri . '/blog/');

but this has no effect on anything.

$server_base_uri is pulled from the $_SERVER variable, so we always have the right sub-domain defined, as the code moves from dev to staging and then to production.


Abdelhadi Touil comments:

does the blog work correctly when you'v installed it the first time on dev.site2.mysite.com?


Lawrence Krubner comments:

Yes, it worked correctly on dev.site2.mysite.com. But when we pushed it to staging, the style sheet and Javascript links remained hardcoded to dev.site2.mysite.com.


Abdelhadi Touil comments:

Try to do the following steps:
1)Export the blog database via phpMyadmin.
2) Open the sql database file using a file editor, I prefer Notepad++, then find and replace dev.site2.mysite.com by admin.mysite.com (find the old url and replace it by the new one); and save changes to database .sql file.
3) Via phpMyadmin Drop all tables from the wordpress database (it's better to have a backup).
4) import the .sql file modified with new url.
5) Try your website.
Here is a detailed tutorial:

[[LINK href="http://www.inboundnow.com/the-ultimate-guide-to-cloning-a-wordpress-site-from-one-url-to-another/"]]http://www.inboundnow.com/the-ultimate-guide-to-cloning-a-wordpress-site-from-one-url-to-another/[[/LINK]]

You can also find and replace using a plugin as Francisco Javier Carazo Gil said.
Good luck.


Lawrence Krubner comments:


Huh, well, ideally we would find a way to automate this. We need to do this every time we push something from dev to production, which will probably happen every week for the foreseeable future. And ideally this will work even when I am not around. So, if possible, I would like to avoid manually editing the database.


2012-09-28

Martin Pham answers:

try to reactivate current theme. Or copy it to a new folder in wp-content/themes / and activate it.


Lawrence Krubner comments:

Hmm, are you suggesting that theme reads its SITE_URL only when it is first activated?