30

Usually in my PHP apps I have a base URL setup so I can do things like this

<a href="<?php echo BASE_URL; ?>tom/jones">Tom</a>

Then I can move my site from development to production and swap it easily and have the change go site wide (and it seems more reliable than <base href="" />.

I'm doing up a WordPress theme, and I am wondering, does WordPress have anything like this built in, or do I need to redefine my own?

I can see ABSPATH, but that is the absolute file path in the file system, not something from the document root.

Maxime
  • 7,283
  • 4
  • 48
  • 49
alex
  • 438,662
  • 188
  • 837
  • 957

5 Answers5

46

get_bloginfo('wpurl'); would be the preferred method of getting the base url of your WordPress installation. This always returns the absolute base url for the install where as get_bloginfo('url'); is for the actual blog address of your WordPress install.

hsatterwhite
  • 6,502
  • 5
  • 24
  • 29
  • 2
    In wordpress 3.5.1 I had to put bloginfo('url'); - without "get_" – Volodymyr Krupach May 21 '13 at 10:54
  • 10
    get_bloginfo() returns the url whereas bloginfo() will output the url. So echo get_bloginfo('url') ?> is the same as bloginfo('url') ?> – Sy Holloway Jun 11 '13 at 13:11
  • 2
    @Sy Holloway I find that I get different outputs for those, actually. bloginfo() seems to output an actual base, in my case "http://localhost", whereas get_bloginfo outputs the site base, "http://localhost/directory". – Nathan Hornby May 28 '15 at 09:57
19

Yes, you can use get_bloginfo('url') just like that or define a constant...

define('BASE_URL', get_bloginfo('url'));

If you are working on a template and want the URL fragment to that theme folder, use...

bloginfo('template_directory'); 
alex
  • 438,662
  • 188
  • 837
  • 957
4

Yes you can get "base URL" with a simple function.

<?php echo get_bloginfo('url') ?>

after that with / you can reach to any page just type the page name.

Shwet
  • 1,740
  • 1
  • 22
  • 34
3

You can try using

<?php echo home_url(); ?>

By using this can get site url like www.xyz.com

<?php echo home_url('/contact'); ?>

By using this syntax you will get url like www.xyz.com/contact

Dipak Mahajan
  • 161
  • 1
  • 4
0

You can use the built in wordpress function site_url() which retrieves the URL for the current site.

Take a look at site_url for more details.

EX:

<a href="<?php echo site_url('tom/jones'); ?>">Tom</a>
kuroyza
  • 162
  • 1
  • 3
  • 12