Question

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.

Was it helpful?

Solution

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 ) );

OTHER TIPS

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.

Why not just use?

get_permalink( get_the_ID() );

That is for single pages.

For category pages, use this:

get_category_link( get_query_var( 'cat' ) );

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.

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();

In my case, this code worked fine:

$current_url = home_url($_SERVER['REQUEST_URI'])

I hope it will help someone, I tried all answers but this one was helpful.

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 ) )

Maybe wp_guess_url() is what you need. Available since version 2.6.0.

This is what worked for me (short and clean solution that includes the query strings in the URL too):

$current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );

The output URL will look like below

http://sometesturl.test/slug1/slug2?queryParam1=testing&queryParam2=123

The solution was taken from here

I realize this is an old question, however one thing I've noticed is no-one mentioned using get_queried_object().

It's a global wp function that grabs whatever relates to the current url you're on. So for instance if you're on a page or post, it'll return a post object. If you're on an archive it will return a post type object.

WP also has a bunch of helper functions, like get_post_type_archive_link that you can give the objects post type field to and get back its link like so

get_post_type_archive_link(get_queried_object()->name);

The point is, you don't need to rely on some of the hackier answers above, and instead use the queried object to always get the correct url.

This will also work for multisite installs with no extra work, as by using wp's functions, you're always getting the correct url.

global $wp;
$wp->parse_request();
$current_url = home_url($wp->request);

After so much research of a simple task, a mix of all answers above works for us:

function get_wp_current_url(){
    global $wp;
    if('' === get_option('permalink_structure')) return home_url(add_query_arg(array($_GET), $wp->request));
        else return home_url(trailingslashit(add_query_arg(array($_GET), $wp->request)));
}

No missing slash at the end and so on. As the question id about output the current url, this doesnt care about security and stuff. However, hash like #comment at the end cant be found in PHP.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top