I am struck in getting the URI in my wordpress application and lack of PHP knowledge is making my progress slow.
I have this URL
http://abc.com/my-blog/abc/cde
i need to create a URL something like
http://abc.com/my-blog/added-value/abc/cde
where http://abc.com/my-blog
is the URL of my wordpress blog which i can easily get using following method
home_url()
i can use PHP $_SERVER["REQUEST_URI"]
to get request URI which will come up as
/my-blog/abc/cde
and than i have no direct way to add value as per my requirement
is there any way to achieve this easily in PHP or WordPress where i can get following information
- Home URL
- Rest part of the URL
so that in end i can do following
Home-URL+ custom-value+Rest part of the URL
My point of Confusion
- On my local set up
$_SERVER["REQUEST_URI"]
is giving me/my-blog/abc/cde
, where/my-blog
is installation directory of wordpress and i can easily skip first level. - On production server its not same as
/my-blog
will not be part of the URL.
Very briefly:
OK, so if
$addition
is the bit you want in the middle and$uri
is what you obtain from$_SERVER["REQUEST_URI"]
then this..Should print:
You might want to use explode or some other sting function. Some examples below:
This is a reliable way to get current url in PHP .
You can store the home url in a variable, using wordpress, using get_home_url()