get_home_path() vs ABSPATH and get_home_url() vs get_site_url() in WordPress

Many WordPress developers are often confusing between get_home_path() and ABSPATH. Same way, They are confused between get_home_url() and get_site_url(). Both codes give the same result in normal WordPress installation. But both codes give different results in a special WordPress installation site where WordPress core files located at a different place than web site’s root folder.

Here, I am going to explain what is difference between get_home_path() and ABSPATH by taking an example. To understand it I have made a demo WordPress site in which I have made a different WordPress code sub-directory in subfolder /wordpress. To understand how to move WordPress code into a different sub-directory you should follow the steps described in https://wordpress.org/support/article/giving-wordpress-its-own-directory/#method-ii-with-url-change. I have given a different WordPress sub-directory to my WordPress site. I have used Method 2 described in the given link.

I have added below code in the wp-content/mu-plugins/test.php file:

<?php
add_action( 'admin_init', function() {

    echo 'Home Path: '.get_home_path();
    echo '<br>';
    echo 'ABSPATH: '.ABSPATH;

    echo '<br><br>';

    echo 'Home URL: '.get_home_url();
    echo '<br>';
    echo 'Site URL: '.get_site_url();
    

    die;
} );

The output of the script is as below:

Home Path: //Users/prashant/Local Sites/wpsepdir/app/public/
ABSPATH: /Users/prashant/Local Sites/wpsepdir/app/public/wordpress/


Home URL: http://wpsepdir.local
Site URL: http://wpsepdir.local/wordpress

The get_home_path() gives root path of WordPress site. The `ABSPATH` gives the path of subdirectory where WordPress codes reside.

The get_home_path() gives the URL where the user can visit the site. The get_site_url() gives URL where the WordPress codes reside.

Here, Home word refers to site’s home. The Home is root folder of the site. Whereas site and ABSPATH refers to WordPress subdirectory where WordPress core code exists.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prashant Baldha