0
votes

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?

2

2 Answers

0
votes

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

0
votes

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.