Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a "base URL" in WordPress within a template file?

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.

like image 810
alex Avatar asked May 17 '10 10:05

alex


People also ask

How do I add a base URL in WordPress?

To set urls in your wordpress application just go to Settings tab at left hand side and go to general tab and set site address(url). Show activity on this post. Show activity on this post. After moving your site files (if necessary), log into your your WordPress Dashboard as an administrator.

How do I find the URL of a base URL?

There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost(). The string parsing way might be easier and let you avoid stuffing everything in a try/catch block.

What is the difference between home URL and site URL?

Home URL: The WP_HOME constant corresponds to the WordPress Address (URL) input field in the Admin. It is used to determine the result of the home_url API function. Site URL: The WP_SITEURL constant corresponds to the Site Address (URL) input field in the Admin.


2 Answers

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.

like image 132
hsatterwhite Avatar answered Sep 21 '22 14:09

hsatterwhite


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');  
like image 22
alex Avatar answered Sep 23 '22 14:09

alex