0

I got it to work with this:

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

However, if the permalink is plain, all I'm getting is the homepage url (instead of the post url).

So what's the best way to get the current post or page address link?

  • You can either use get_permalink() or use jQuery if it's something dynamic activity you're looking for. var pathname = window.location.pathname; // Returns path only (/path/example.html) or var url = window.location.href; // Returns full URL (https://example.com/path/example.html) or var origin = window.location.origin; // Returns base URL (https://example.com) – Vishwa May 20 at 4:01
  • @Vishwa was hoping for a php way : ) – mrKC.988 May 20 at 4:16
  • get_permalink() should work for you. if you get problems in homepage, you can check with is_page(home). however, for your code to work properly, you'll need global $wp; before your code – Vishwa May 20 at 4:40
  • Also try this global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request)); – Vishwa May 20 at 4:43
  • Possible duplicate of How to get URL of current page displayed? – leymannx May 20 at 6:49
1

Is there a reason why get_permalink() doesn't work for you? I'm unclear why you are trying to construct it manually.

  • this doesn't work for homepage, for example: http://localhost/m/ is giving me http://localhost/m/2019/05/18/bodev/ I need the exact URL on the address bar to do a sensitive comparison. – mrKC.988 May 20 at 4:12
  • 1
    What about leveraging the $_SERVER variable instead? You should be able to replace $wp->request with $_SERVER["REQUEST_URI'] – Derek Held May 20 at 4:36
  • +1 Normally global $wp; if ($wp->request == 'myslug) { /** Do something */ } or if (is_home) { /** Do something */ } should just be fine. No need to reinvent the wheel. – leymannx May 20 at 6:48
  • get_permalink() is only for use within the loop and gets the permalink for the current post in the loop. It does not work reliably outside the loop, and does not work for archives or any page that's not a single post. – Jacob Peattie May 28 at 8:14
0

Don't use get_permalink(). That function is intended for use inside the loop, and only works for getting the link to posts. It will not work for taxonomy archives, date archives, search results, or the latest posts page.

Normally you can get the URL to the current page with home_url() combined with $wp->request:

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

This is because home_url() gets your site URL from the database, while $wp->request holds the path for the current request, which will be the slug for pages, date for archives, etc.

The problem with plain permalinks is that they don't use the request path, because this requires the standard .htaccess setup. Instead WordPress uses the query string to determine what to load.

So what you need to do is get the current query vars and append those to the URL. Thankfully those are also available in $wp, specifically as $wp->query_vars. This is an array of vars and values, so you can pass it directly to add_query_arg();:

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

Yeap, this should't work with plain format. and also just get_permalink() also not going to solved your issue here. To solved your issue you need to first correct Page/Post id and WordPress default doesn't offer us any function to do that. So you need to make one to grab the correct id. example

/**
 * Get correct page or post id
 *
 * @return int
 */
if ( !function_exists( 'get_the_real_ID' ) ) {
    function get_the_real_ID() {
      $post_id = 0;
      if (in_the_loop()) {
           $post_id = get_the_ID();
      } else {
        if ( is_front_page() && !is_home() ) {
            $frontpage_id = get_option( 'page_on_front' );
            $post_id = $frontpage_id;
        } elseif ( !is_front_page() && is_home() ) {
            $page_for_posts = get_option( 'page_for_posts' );
            $post_id = $page_for_posts;
        } else {
            global $wp_query;
            $page_object = $wp_query->get_queried_object();

            if ( is_page() || is_singular() ) {
                $post_id = $wp_query->get_queried_object_id();
            } else {
                $post_id = 0;
            }
        }
      }
      return $post_id;
    }
} 

Now just use this ID to grab your page/post url like this.

$id = get_the_real_ID();
$current_url = (empty($id)) ? home_url() : get_permalink($id);

I'm not sure that this is the best or perfect solution for your case or not but it should at-least will be work as you expected. I assume that you might change the permalink format or want to grab the proper url whatever format used? and uppon that I share this method. try and let me know.

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.