Getting the sub folder install location for WordPress if its not installed in the site root
Simple home_url() method
wp_redirect( home_url( '/' ) );
//wp_redirect( home_url( '/my_page_name' ) ); //If you want to redirect to a specific page
die();
Getting it in code
//Get WordPress sub folder path if its not instlaled on the site root
$SiteUrl = get_site_url();
$ParsedUrl = parse_url($SiteUrl);
$WordpressInstallSubPath = trim($ParsedUrl['path'] ?? '', '/'); //Trim leading/trailing slashes and return full path (if any)
if (strlen($WordpressInstallSubPath) > 0)
$WordpressInstallSubPath = '/' . $WordpressInstallSubPath;
//Install URL $WordpressInstallSubPath
//https://example.com '' (empty — root)
//https://example.com/wordpress '/wordpress'
//https://example.com/blog/wp '/blog/wp'
//https://example.com/a/b/c '/a/b/c'
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.