Hirazi Webtech

Differentiate WordPress Staging vs Production Sites on SiteGround Hosting

Importance of staging can’t be overstated. Whether you are planning a big launch or modifying bits and pieces, you have to do a sanity check on staging server to see everything will execute as planned on the D-day! And if you are hosting with SiteGround, our affiliate partners, you are lucky! Good folks at SiteGround does that for you with a click of a button.

 

What it doesn’t do is to have a proper bifurcation between the two environments. We faced one such instance at Hirazi Webtech when we noticed our analytical reports were showing incorrect numbers. The reason behind the discrepancy was home_url(), which doesn’t work as usual on SiteGround hosting. The value it returns is the main home url (which in this case was http://hirazi.com) even in the testing environment which ideally should have been http://staging1.hirazi.com. This lack of differentiation between the two servers was causing our GA numbers to bump up.

 

The Problem
Unable to differentiate between WordPress staging and production sites when using SiteGround hosting.

 


$domain = parse_url( home_url(), PHP_URL_HOST ); if ( false !== strpos( $domain, 'hirazi.com' ) ) { if( false !== strpos( $domain, 'staging1' ) ) { define( 'HIRAZI_ENVIRONMENT', 'staging' ); } else { define( 'HIRAZI_ENVIRONMENT', 'production' ); } } else { define( 'HIRAZI_ENVIRONMENT', 'dev' ); }

 

Hirazi in Debugging Mode…

We spent hours diagnosing the fact that what’s wrong with the logic. It turns out that

 

“the tool actually doesn’t replace your „live“ domain with the new one for staging. Rather, SiteGround has created a special Apache module which virtually routes the requests to the correct address” (Check out the full tutorial on SiteGround Staging).

 

After spending some time on Google, we found the article in WordPress Codex which explains the intricacies of running a development copy of your WordPress website.

 

“WordPress serves pages with embedded absolute URLs based on the absolute site URL configured in your database, so none of the links on your development site will work.”

 

The Solution!

After the research was successful, it was easy to understand what’s going on and why our GA tracking is showing up on staging! We fixed it by using $_SERVER[‘SERVER_NAME’]

 


$domain = $_SERVER['SERVER_NAME']; if ( false !== strpos( $domain, 'hirazi.com' ) ) { if( false !== strpos( $domain, 'staging1' ) ) { define( 'HIRAZI_ENVIRONMENT', 'staging' ); } else { define( 'HIRAZI_ENVIRONMENT', 'production' ); } } else { define( 'HIRAZI_ENVIRONMENT', 'dev' ); }

 

As it turns out, SiteGround staging tool is great as it does all the legwork for you. But, incase you stumbled upon the similar issue, make this small change and you are Golden!

Link to social platforms of Hirazi Webtech

We like to spread happiness. Let's do it together.

Contact details of Hirazi Webtech

Get in Touch

Would love to hear you over coffee!

Contact Detail

Hirazi Webtech

Mumbai, India

+91 96994-53388

contact.us@hirazi.com