0

I'm using a localhost site as the basis for creating other sites from. I'm using php to generate internal links by finding the current domain, and displaying it with a shortcode. I intend to upload my site to a live host and therefore the domain will change, and my internal links won't be broken.

//add shortcode that displays current site name
function GR_site_name(){
$currentDomain = $_SERVER['HTTP_HOST'];
return $currentDomain.'/wordpress';
}

add_shortcode('GR_site_name', 'GR_site_name');
?>

I've tested this by adding the code to live sites and it works perfectly.

I've read the post at how safe is $_SERVER["HTTP_HOST"]? however it details it as 'generally unsafe' due to the script that it runs.

Is $_SERVER['SERVER_NAME'] safer than $_SERVER['HTTP_HOST']? Or are both regarded as bad practice?

G Ritchie
  • 146
  • 2
  • 12
  • Possible duplicate of [how safe is $\_SERVER\["HTTP\_HOST"\]?](https://stackoverflow.com/questions/10350602/how-safe-is-serverhttp-host) – MackProgramsAlot Apr 09 '18 at 13:49

2 Answers2

0

Make a config file with define('host','https://example.com') and use host wherever you want and when you upload script to server just change url in config file and you're good to go

  • We can't trust anything that comes from client! Hope it solves your problem and in case if it still persists feel free to ask –  Apr 09 '18 at 14:15
  • I've added define('host','https://example.com'); to my config file but the shortcode is still returning the same value, meaning it will still use the value defined by client. How do I fix this? – G Ritchie Apr 09 '18 at 14:24
  • Managed to work it out, was defining too early. I've added $_SERVER['HTTP_HOST']='example.com'; underneath define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress'); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress'); and it's working now. – G Ritchie Apr 09 '18 at 14:29
0

Use a combination of the standard WP_HOME & WP_SITEURL constants, along with the site_url() function.

I'd recommend using one local copy of wp-config.php and a different one for the live site, and then add the following to them:

define('WP_SITEURL', 'http://www.example.com/wordpress');   // Or 'http://localhost/wordpress' for the local copy
define('WP_HOME', 'http://www.example.com');

That way, you don't need a custom function or shortcode - you can simply call site_url() wherever you need it to get the URL of your WP site.

Alternatively, if you need to keep the site URL variable and to accept anything that points to it:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');

If you need to keep the shortcode for use within the context of a post, you can change it to:

function GR_site_name(){
    return site_url();
}

Note that setting WP_HOME and WP_SITEURL override any database setting for the site's URL, and if you visit the Settings->General page, you'll see those two fields are greyed out.

indextwo
  • 5,535
  • 5
  • 48
  • 63