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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $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 ) ) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.