Skip to main content
Share Your Experience: Take the 2024 Developer Survey
227 votes
Accepted

How to get URL of current page displayed?

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 ...
Jacob Peattie's user avatar
67 votes

How do you retrieve a post by slug name through REST API?

Posts: sitename.com/wp-json/wp/v2/posts?slug=post-slug Pages: sitename.com/wp-json/wp/v2/pages?slug=page-slug Custom post type: sitename.com/wp-json/wp/v2/POST_TYPE?slug=post-slug
Runnick's user avatar
  • 1,009
33 votes

What's the difference between home_url() and site_url()

##TLDR: In a non-standard installation, you can place your WordPress files in a subdirectory of your website root. ...and still allow your visitors to visitors to access your WordPress "website&...
SherylHohman's user avatar
32 votes

Remove slug from custom post type post URLs

Write following code into the taxonomy registration. 'rewrite' => [ 'slug' => '/', 'with_front' => false ] Most important thing that you have to do after code changing After you’ve ...
Mayank Dudakiya's user avatar
31 votes

How to get URL of current page displayed?

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 ...
rescue1155's user avatar
30 votes

Remove slug from custom post type post URLs

Looking through the answers here I think there is room for a better solution that combines some things I learned above and adds auto-detection and prevention of duplicate post slugs. NOTE: Make sure ...
Matt Keys's user avatar
  • 806
28 votes
Accepted

Stop Wordpress appending `-2` to the end of my url after i change it

I know this topic is old, but I was having the same issue, and none of the suggested fixes worked. I didn't have any pages still in trash. I didn't have a category with the same name as my page. I ...
Ed Selby's user avatar
  • 396
28 votes
Accepted

How does WordPress create URLs that Apache knows about?

Actually there is no communication happening between Apache and WordPress. The "magic" is happening in Apache mod_rewrite rules. For a standard WordPress installation, you have the following rules in ...
Fayaz's user avatar
  • 8,927
21 votes

How to get URL of current page displayed?

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' ) ); Simple script to get the current URL of any page:...
Dario Zadro's user avatar
19 votes

How to get URL of current page displayed?

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.
Akmal's user avatar
  • 291
18 votes
Accepted

Access post from post id in URL

Post can be accessed using post id by passing it as page_id query param to index.php. e.g. http://wordpress.loc/index.php?page_id=16 Or simply http://wordpress.loc/?page_id=16
Abdul Rauf's user avatar
16 votes

How to add a Custom Link to a Menu with a URL that is relative to the blog URL

I was also looking for a solution for this and I found simple one. This is what you need to place into the URL field: /index.php/internal-site-name It works just fine! Best Regards Chris
Christian Hatz's user avatar
16 votes

Remove slug from custom post type post URLs

Plugin Roundup It's almost 2020 and a lot of these answers don't work. Here's my own roundup of the current options: Matt Keys answer seems to be the only one on the right track if you want a custom ...
squarecandy's user avatar
13 votes
Accepted

Get current URL (permalink) without /page/{pagenum}/

You can get the current URL through home_url( $wp->request ). Try the example below: global $wp; // get current url with query string. $current_url = home_url( $wp->request ); // get the ...
GKS's user avatar
  • 1,306
13 votes
Accepted

Change default URL path (/blog/) of blog posts

You can create any structure you want for this on the permalinks settings page in the dashboard. To set it to your example of /blog/title-of-post Simply navigate to Settings -> Permalink. Select ...
Dane Morgan's user avatar
12 votes

Get current URL (permalink) without /page/{pagenum}/

Actually the easiest would be to use get_pagenum_link() which will return you the current URL without any /page/* paramaters. Bonus You can also simply use it to build "Previous" and "Next" links ...
leymannx's user avatar
  • 3,161
11 votes

How to prevent automatic redirection of 404 errors and "incorrect" URLs?

As Ash suggested, you can turn off the feature by using the following code: remove_action('template_redirect', 'redirect_canonical'); In looking at the redirect_canonical function in canonical.php, ...
Mike J-P.'s user avatar
  • 211
11 votes

How to get URL of current page displayed?

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 = ...
Adiyya Tadikamalla's user avatar
10 votes

Use a template file for a specific url without creating a page

The init action isn't appropriate for what you're trying to achieve. You should be using the template_include filter instead. You'd combine this with get_query_var to retrieve the URL params to check ...
adamj's user avatar
  • 212
10 votes

How to convert the file path to a URL of the same file?

Or global function abs_path_to_url( $path = '' ) { $url = str_replace( wp_normalize_path( untrailingslashit( ABSPATH ) ), site_url(), wp_normalize_path( $path ) ); ...
Serge Liatko's user avatar
10 votes

Get current URL (permalink) without /page/{pagenum}/

Answer by Govind Kumar worked, however, it only returned the URL if /page/{pagenum}/ was present in the URL and returned nothing if not. I needed a universal solution that will always return the base ...
Ihor Vorotnov's user avatar
10 votes
Accepted

paginate_links ignore my format

This part: 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ), is generating a page part like this one: 'base' => http://example.tld/page/%#%/ If we peek ...
birgire's user avatar
  • 67.4k
9 votes

Redirecting to old domain after migration

Run the following when you are changing domain: UPDATE wp_options SET option_value = 'https://yournew.domain' WHERE option_name = 'siteurl'; UPDATE wp_options SET option_value = 'https://yournew....
crmpicco's user avatar
  • 831
9 votes
Accepted

Encode text string being appended as query to URL

To encode the URL, you could use the PHP urlencode( $url ) function or use the WordPress urlencode_deep( $array | $str ); function. add_shortcode( 'dynamic_contact_button', 'button_product_page' );...
John Zenith's user avatar
9 votes
Accepted

Why does WordPress automatically redirect URLs with the parameter "name=" to a different page?

In WordPress, name is a reserved term (emphasis mine): There is a complete set of reserved keywords, or terms, in WordPress that should not be used in certain circumstances as they may conflict ...
Jacob Peattie's user avatar
8 votes

I am Looking to append URL Parameter to all URLs

I don't know if you still need the answer, but here's working example for you. Header.php /*SAVE $_GET['aff'] TO COOKIE*/ if(!isset($_SESSION['aff']) and $_GET['aff']){ $cookie_expire = time()+...
Komarovski's user avatar
8 votes

Mixed content warnings after changing site adress from HTTP to HTTPS

This is happening because WordPress saves URLs in content absolutely by default (meaning that it's actually got your urls saved as http://example.com in the database). So to fix this you'll want to ...
Ian's user avatar
  • 969
8 votes
Accepted

Create Custom URL structure for specific Post category

Try these steps: Step #1: Replace this: add_action('generate_rewrite_rules', 'custom_rewrite_rules'); function custom_rewrite_rules( $wp_rewrite ) { $regex = '/[\s\S]/'; $new_rules = array( ...
Sally CJ's user avatar
  • 39.3k
7 votes

How does WordPress generate URL slugs?

You can use this function: static public function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\pL\d]+~u', '-', $text); // transliterate $text = iconv('utf-...
Vivek Sancheti's user avatar
7 votes

Remove slug from custom post type post URLs

Background Even after looking around everywhere, I couldn't find a proper solution for removing CPT slug from permalinks that actually works and is consistent with how WordPress actually parses ...
arafatgazi's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible