How to get current page URL and title in WordPress?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here are few super global variables for getting url info:
$_SERVER[‘SCRIPT_NAME’]
$_SERVER['SCRIPT_NAME']will be same –/index.php. It is irrespective of the actual URI ($_SERVER['REQUEST_URI']) used to access the site.As it returns the actual script name, it fails provide additional path information that may be present. So if the
$_SERVER['REQUEST_URI']is/index.php/big/directory/then too the$_SERVER['SCRIPT_NAME']will be same –/index.php.$_SERVER['SCRIPT_NAME']is supported on all platforms$_SERVER[‘PHP_SELF’]
This is the filename of the currently executing script, relative to the document root. However, unlike
$_SERVER['SCRIPT_NAME'], it provides additional path information like$_SERVER['REQUEST_URI']when the actual php file is present in the path. So when the$_SERVER['REQUEST_URI']is/index.php/big/directory/then$_SERVER['PHP_SELF']will be/index.php/big/directory/.However if all the URI’s under
http://www.example.com/is mapped tohttp://www.example.com/index.php, then, for example,http://www.example.com/abc/defwill return/index.phplike$_SERVER['SCRIPT_NAME']. Note that$_SERVER['REQUEST_URI']data is ignored for this request.$_SERVER[‘REQUEST_URI’]
It will give you entire url including query string vars.
Title of the page:
There is no built-in functionality to get title of the page, however, you can use the HTML Simple DOM to read the contents of
<h2.Example:
Now, you can use
$titlehowever, you like. Visit the their site for more info about it.