WordPress Path Functions: List WordPress Theme, Plugin, Site URL and Absolute Path in Server – WordPress Tutorial

By | April 4, 2020

When we are developing a wordpress theme or plugin, we have to get some wordpress path information. such as the absolute path of wordpress theme, plugin, style file or home url. In this tutoral, we will introduce some wordpress path functions to get these path or url information.

About wordpress site

home_url()

It will return the home url of a site. For example:

<?php
$url = home_url();
echo $url;
//output: http://www.example.com

$url = home_url('/linux/');
echo $url;
//output: http://www.example.com/linux/
?>

To learn more on home_url(), you can read this tutorial.

Understand WordPress home_url() for Beginngers – WordPress Tutorial

site_url()

This function returns the home url of a wordpress site, which may be not the same to home_url(). For example, if you created a wordpress site in a blog folder. You will see:

$url = site_url();
echo  $url;
//output:http://www.example.com/blog

$url = home_url();
echo  $url;
//output:http://www.example.com

admin_url()

It returns the wordpress dashboard admin url.

$url = admin_url();
echo $url;
//output: http://www.example.com/wp-admin/

content_url()

This function returns the url of wp-content folder.

$url = content_url();
echo  $url;
//output: http://www.example.com/wp-content

includes_url()

This function returns the url of wp-includes folder.

$url = includes_url();
echo $url;
//output: http://www.example.com/wp-includes/

$url = includes_url( '/js/');
echo $url;
//output: http://www.example.com/wp-includes/js/

wp_upload_dir()

It returns the url of wordpress uploads folder.

$upload_dir = wp_upload_dir();
echo $upload_dir['baseurl'];
//output: http://www.example.com/wp-content/uploads

About wordpress theme path function

get_theme_root_uri()

It returns the url of wordpress themes folder.

<?php
echo get_theme_root_uri();
//output: http://www.example.com/wp-content/themes

get_theme_root()

This function returns the absolute path of themes folder in server.

<?php
echo get_theme_root();
//output: /home/user/public_html/wp-content/themes
?>

get_stylesheet_directory()

This function returns the absolute path of current wordpress theme in server.

<?php
echo get_stylesheet_directory();
//output: /home/user/public_html/wp-content/themes/twentyeleven
?>

get_stylesheet_directory_uri()

This function returns the url of current theme.

<?php
echo get_stylesheet_directory_uri();
//output: http://www.example.com/wp-content/themes/twentyeleven
?>

About wordpress plugin path function

plugins_url()

This function returns the url of  plugins folder.

<?php
echo plugins_url();
//output: http://www.example.com/wp-content/plugins
?>

plugin_dir_url()

This function is same to plugins_url().

<?php
echo plugin_dir_url(__FILE__ );
//output: http://www.example.com/wp-content/plugins
?>

plugin_dir_path()

It returns the absolute path of current plugin folder in server.

<?php
echo plugin_dir_path(__FILE__ );
//output:/home/user/public_html/wp-content/plugins/myplugin/
?>

Leave a Reply