Reputation: 11317
I am currently fixing a wordpress theme that contains absolute URLs. Example:
$.preloadImages('/wp-content/themes/themeName/img/button.png');
Now I want to prefix the url with something like WP_HOME
or WP_SITEURL
but I don't know which one to choose. What is the exact difference? In other words, what is the scenario in which the two might differ?
I guess they correspond to the "wordpress address" and the "site address" in the settings. However, the explanation there does not help me.
Upvotes: 0
Views: 2914
Reputation: 943
home_url : Its your Home URL for your site.
site_url : Where your wordpress Core files are located.
For your use you can use site_url this will work for you.
Upvotes: 1
Reputation: 2744
href="<?php echo get_template_directory_uri(); ?>/img/button.png"
so use get_template_directory_uri()
DIFFERANCE
<?php echo get_template_directory(); ?>
Returns an absolute server path (eg: /home/user/public_html/wp-content/themes/my_theme), not a URI.
2.get_template_directory_uri ()
Retrieve theme directory URI.
www.example.com/wp-content/theme/themename/image/image.png
AS per your query. if its for plugin then use plugin_url
or for theme get_template_directory_uri()
get_template_directory_uri() . '/js/custom-script.js'
Here are other functions.
SITE URL
the site_url
template tag retrieves the site url for the current site (where the WordPress core files reside) with the appropriate protocol, 'https
' if is_ssl()
and 'http
' otherwise. If scheme is 'http
' or 'https
', is_ssl()
is overridden.
Use this to get the "WordPress address" as defined in general settings. Use home_url()
to get the "site address" as defined in general settings.
$url = site_url();
echo $url;
Output: http://www.example.com or http://www.example.com/wordpress
HOME URL
The home_url
template tag retrieves the home URL for the current site, optionally with the $path argument appended. The function determines the appropriate protocol,
$url = home_url();
echo $url;
Output: http://www.example.com
So mostly both are similar. not different. you cal also use / in both to get / in output.
BLOG INFO
There is another one is blog info
function bloginfo( $show='' ) {
echo get_bloginfo( $show, 'display' );
}
<?php bloginfo('url'); ?> it will also return blog url .
Upvotes: 1
Reputation: 3220
I would use neither. Use a function instead of a constant get_bloginfo('url');
or get_bloginfo('wpurl');
. If you have your wp-content
inside of the wordpress install it does not really matter (but I prefer the first).
Take a look at the following functions: content_url()
, get_template_directory_uri()
(for parent themes) or get_stylesheet_directory_uri()
(for themes including child themes). When using these functions you don't rely on one particular directory structure.
In your case I'd use
trailingslashit(get_stylesheet_directory_uri()) . 'img/button.png';
EDIT
WP_SITEURL
is your WordPress address (wp_url
) and WP_HOME is your blog address (url
). By default the wp-content
directory is a sibling to wp-admin
and wp-includes
and both constants have the same value but you move it out of there and make use of the WP_CONTENT_FOLDERNAME
, WP_CONTENT_DIR
and WP_CONTENT_URL
constant to move it somewhere else, e.g.
wp-content
index.php
wordpress
|-- wp-admin
|-- wp-includes
now WP_SITEURL
will return https://yourdomain.com/wordpress/
and WP_HOME
will be https://yourdomain.com/
, see https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory for more details of giving WordPress its own directory.
Upvotes: 1