43

I want to add custom PHP code to ensure that whenever a page on my site loads in my browser, the URL of that page is echoed to the screen. I can use echo get_permalink(), but that does not work on all pages. Some pages (e.g. my homepage) display several posts, and if I use get_permalink() on these pages, the URL of the displayed page is not returned (I believe it returns the URL of the last post in the loop). For these pages, how can I return the URL?

Can I attach get_permalink() to a particular hook that fires before the loop is executed? Or can I somehow break out of the loop, or reset it once it is complete?

Thanks.

  • Can you provide some context as to what you would want to do with this URL? Are you trying to create sharable URLs? Assemble custom URLs for links/actions? – Tom J Nowell Jul 25 '17 at 12:19
  • @TomJNowell I would like to enqueue a particular JS script, but only on certain pages (in this case, those pages are the homepage of my site in various languages: example.com, example.com/fr, example.com/es, etc). The JS file will server to add hyperlinks to several titles that appear only on the homepage. – cag8f Jul 25 '17 at 16:03
  • why not just use is_home() or is_page( 'fr' ) etc and only enqueue the script if it's true? – Tom J Nowell Jul 25 '17 at 16:15
  • @TomJNowell Well now my code is if ( home_url( $wp->request ) == home_url() ) { wp_enqueue_script();} This appears to fire on every home page, regardless of language. Is that what you were suggesting? – cag8f Jul 26 '17 at 5:32
  • Why not use $_SERVER['REQUEST_URI'] and company? See this question: stackoverflow.com/q/6768793/247696 – Flimm May 29 at 13:07
71

get_permalink() is only really useful for single pages and posts, and only works inside the loop.

The simplest way I've seen is this:

global $wp;
echo home_url( $wp->request )

$wp->request includes the path part of the URL, eg. /path/to/page and home_url() outputs the URL in Settings > General, but you can append a path to it, so we're appending the request path to the home URL in this code.

Note that this probably won't work with Permalinks set to Plain, and will leave off query strings (the ?foo=bar part of the URL).

To get the URL when permalinks are set to plain you can use $wp->query_vars instead, by passing it to add_query_arg():

global $wp;
echo add_query_arg( $wp->query_vars, home_url() );

And you could combine these two methods to get the current URL, including the query string, regardless of permalink settings:

global $wp;
echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );
  • 6
    If permalinks set to plain: echo '//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];. – Max Yudin Jul 25 '17 at 12:18
  • @Jacob I tried that, but it seems to be returning the URL of my homepage only. As you can see in the top left on this page (dev.horizonhomes-samui.com/properties/hs0540), where I have inserted code to echo home_url( $wp->request ). I have ensured to include global $wp as well. Permalinks are not 'Plain,' but set to 'Post Name.' I don't see any relevant PHP errors in the log either. This particular page is part of my dev site, which is otherwise blocked off to visitors. I'm not sure if that matters or not. edit: Actually, hold that thought--could be user error. Stand by... – cag8f Jul 25 '17 at 16:14
  • @Jacob edit 2: OK your code does indeed work. My issue was that I was including the code in functions.php 'naked,' i.e. not in any function that is attached to a hook. So your code was returning the URL of the homepage, regardless of the page that was displayed in my browser. Once I moved the code inside a function--a function attached to a WP hook (wp_enqueue_scripts), it did indeed return the URL of the displayed page. Do you know the reason for that behavior? Maybe I need to create a new question for that. – cag8f Jul 25 '17 at 16:20
  • 2
    @cag8f If the code sits "naked" in functions.php then you are running it before all the properties of the $wp object have been set up. When you wrap it in a function attached to an appropriate hook then you are delaying its execution until a suitable point in the Wordpress code run. – Andy Macaulay-Brook Apr 5 '18 at 11:28
6

You may use the below code to get the whole current URL in WordPress:

global $wp;
$current_url = home_url(add_query_arg(array(), $wp->request));

This will show the full path, including query parameters.

2

The following code will give the current URL:

global $wp;
echo home_url($wp->request)

You can use the below code to get the full URL along with query parameters.

global $wp;  
$current_url = home_url(add_query_arg(array($_GET), $wp->request));

This will show the full path, including query parameters. This will preserve query parameters if already in the URL.

1
function current_location()
{
    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
    } else {
        $protocol = 'http://';
    }
    return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

echo current_location();
  • Can you explain how and why this code solves the question? – kero Nov 29 '18 at 11:29
  • In my opinion the most flexible solution. It works on any WP page (even on wp-admin, wp-login.php, archive pages, etc). Just notice, that it does not include any URL params – Philipp Apr 1 at 16:09
0

This is an improved way of example that mentioned previously. It works when pretty URLs are enabled however it discards if there is any query parameter like /page-slug/?param=1 or URL is ugly at all.

Following example will work on both cases.

    $query_args = array();

    $query = wp_parse_url( $YOUR_URL );

    $permalink = get_option( 'permalink_structure' );

    if ( empty( $permalink ) ) {

        $query_args = $query['query'];

    }

    echo home_url( add_query_arg( $query_args , $wp->request ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.