B4b4j1
B4b4j1

Reputation: 460

Returning current URL in WordPress

I am trying to implement canonical and hreflang tags in WordPress, but I cannot retrieve the current URL of the visited page.

I tried :

 <?php echo site_url(); ?>

But it returns https://www.example.com instead of https://www.example.com/current-page1

Upvotes: 5

Views: 14602

Answers (4)

amarinediary
amarinediary

Reputation: 5469

Here are my two cents. The approaches described are using $_SERVER global without verifying the integrity of each query variables.

Unregistered variable/value couples are being displayed even tho they're not part of the WordPress public query environment WP_Query class (recognized by WP_Query).

I would suggest comparing each query variables against the public query variables scope before returning the current permalink.

<?php

/**
 * Safely retrieve the current permalink.
 *
 * @param String $args['relative'] (true|false), Relative path if true. Default to false. 
 *
 * @return String Relative or absolute path to the current permalink.
 * 
 * @since 1.0.0
 *
 * @see https://stackoverflow.com/a/70809779/3645650
 */
function wpso_50761584( $args = array( 'relative' => false ) ) {
        
    global $wp;

    // Parse the current query string into variables
    parse_str( $_SERVER['QUERY_STRING'], $variables );

    // Filter out unregistered query variables
    $filtered_vars = array_filter( $variables, function ( $value, $variable ) {

        return get_query_var( $variable );
        
    }, ARRAY_FILTER_USE_BOTH );

    // Rebuild the URL with the filtered query variables
    $permalink = add_query_arg( $filtered_vars, home_url( $wp->request ) );

    // Make the URL relative if requested
    if ( $args['relative'] ) {

        $permalink = wp_make_link_relative( $permalink );

    };

    return esc_url_raw( $permalink );

}

As an example, you could echo-it-out on the front end:

<?php

echo wpso_50761584( 
    array(
        'relative' => true
    )
);

Upvotes: 2

Jagir bahesh
Jagir bahesh

Reputation: 381

If permalinks set to the plain:

$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Upvotes: 1

Sadiq11111
Sadiq11111

Reputation: 96

In case anyone else needs this. Below code should get the exact URL including parameters.

home_url($_SERVER['REQUEST_URI']);

You can echo or return it depending on your use case. e.g.

echo home_url($_SERVER['REQUEST_URI']);

or

return home_url($_SERVER['REQUEST_URI']);

Upvotes: 8

Frits
Frits

Reputation: 7624

The site_url() function returns the actual website root URL. Depending on where you are calling your URL you could try get_the_permalink() but a more surefire way would be to use the $wp->request method. Like this:

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

The main problem with this function is that the URL parameters are left out, so if your link is something like: http://example.com/test/?myparam=1 it will only return http://example.com/test/

Upvotes: 7

Related Questions