Get the full url in PHP and WORDPRESS
how to get the full url in php and wordpress
these are the outputs of the php tags:-
* To get full url or base url in Wordpress
$_SERVER["DOCUMENT_ROOT"] === /home/user/www
$_SERVER["SERVER_ADDR"] === 124.16.3.3
$_SERVER['HTTP_HOST'] === yoursite.com (or with WWW)
$_SERVER["REQUEST_URI"] === /folder1/folder2/yourfile.php?var=foo#123
__FILE__ === /home/user/www/folder1/folder2/yourfile.php --->//p.s. ON WINDOWS SERVERS, instead of / is \
basename(__FILE__) === yourfile.php
__DIR__ === /home/user/www/folder1/folder2 [same: dirname(__FILE__)]
$_SERVER["QUERY_STRING"] === var=foo#123
$_SERVER["REQUEST_URI"] === /folder1/folder2/yourfile.php?var=foo#123
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) === /folder1/folder2/yourfile.php
$_SERVER["PHP_SELF"] === /folder1/folder2/yourfile.php
//if "YOURFILE.php" is included in "PARENTFILE.php" , and "PARENTFILE.PHP?abc" is opened:
$_SERVER["PHP_SELF"] === /parentfile.php
$_SERVER["REQUEST_URI"] === /parentfile.php?abc
$_SERVER["SCRIPT_FILENAME"]=== /home/user/www/parentfile.php
str_replace($_SERVER["DOCUMENT_ROOT"],'', str_replace('\\','/',__FILE__ ) ) === /folder1/folder2/yourfile.php
You can use this php function to get url in php
function getUrl() {
$url = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
* To get full url or base url in Wordpress
home_url() //> http://example.com
get_stylesheet_directory_uri() //> http://example.com/wp-content/themes/THEME_NAME [same: get_bloginfo('template_url') ]
get_stylesheet_directory() //> /home/www/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__) //> http://example.com/wp-content/plugins/MY-PLUGIN/ [while used inside plugin.. same as: plugins_url('',__FILE__) ]
plugin_dir_path(__FILE__) //> /home/www/wp-content/plugins/MY-PLUGIN/ [while used inside plugin]
//===============MY EXAMPLES - USAGE============//
(i.e. wordpress is installed in subdirectory: http://example.com/wpdir/)
define('domainURL', (((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' ).$_SERVER['HTTP_HOST']);
// -----> http://example.com
define('homeURL', home_url());
// -----> http://example.com/wpdir/
define('homeFOLD', str_replace(domainURL,'', homeURL));
// -----> /wpdir/
define('requestURI', $_SERVER["REQUEST_URI"]);
// -----> /wpdir/any-page?with=parameters
define('requestURIfromHome', str_replace(homeFOLD, '',requestURI) );
// -----> /any-page?with=parameters
define('requestURIWithoutParametr',parse_url(requestURIfromHome, PHP_URL_PATH));
// -----> /wpdir/any-page
define('currentURL', domainURL.requestURI);
// -----> http://example.com/wpdir/any-page?with=parameters
define('THEME_URL', str_replace(domainURL, '', get_template_directory_uri()) );plugin_dir_url(__FILE__)) );
// -----> http://example.com/wpdir/wp-content/themes/THE-THEME-NAME/
define('PLUGIN_URL', str_replace(domainURL, '', plugin_dir_url(__FILE__)) );
// -----> http://example.com/wpdir/wp-content/plugins/THE-PLUGIN-NAME/
Solution 1
$complete_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Solution 2
$complete_url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(dirname(__FILE__)));
get the full url in PHP and Wordpress Is Very Helpful. Thanks for such Information
ReplyDeleteLooking for WordPress Support Visit :
WordPress Support
WordPress Technical Support
WordPress Support Phone Number
Awesome Information
ReplyDelete24/7 WordPress Support
24/7 WP Support