• Assume our base URL is example.com. $_SERVER['SERVER_NAME'] is enough to get this.

    But if it’s in a local development environment ( LDE ) like Xampp, the base URL is localhost/example where the example is the WordPress folder name.

    $_SERVER['SERVER_NAME'] is already not capable as it only returns localhost. But get_option('siteurl') is still capable of returning localhost/example.

    My question is how WordPress know the base URL is localhost/example, as $_SERVER['SERVER_NAME'] only returns localhost.

    Because if I develop a plugin and I use $_SERVER['SERVER_NAME'], apparently it not gonna work in LDE.

    You might ask why not just use get_option('siteurl') then, because I wanna learn how WordPress get that.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @ace0930,

    You are get your site base URL in WordPress using this function easily.
    get_site_url();

    Thread Starter ace0930

    (@ace0930)

    @hirenbhanderi Hi, get_site_url() is also using get_option ( 'siteurl' ) if you look at the source code.

    I’m not asking how to get the base URL like which function to use, I’m asking how WordPress gets the siteurl…Haha

    Okay @ace0930

    The get_option function retrieves the value from the database wp_option table.

    Thread Starter ace0930

    (@ace0930)

    @hirenbhanderi Exactly, but how does WordPress get the base URL localhost/example before putting it into the wp_option table, that’s what I’m asking…

    As I have mentioned that the $_SERVER['SERVER_NAME'] only returns localhost, so I wonder how WordPress finds localhost/example.

    Moderator bcworkz

    (@bcworkz)

    The base URL is set in Settings. If you change the settings, the option changes and so do all of your URLs. Of course if you change it to a non-functional URL nothing will work.

    The base absolute server path is set as the constant ABSPATH in wp-config.php, which comes from PHP’s __DIR__.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How WordPress get the base URL’ is closed to new replies.