I currently have an ID of a page that I want to use its permalink as the front of a permastruct of a CPT I'm setting up.

Now I can use get_permalink() but that returns the full URL:


but all I want to return is imapage/subpage/subsubpage

Is there a function that can do this or do I have to device something that can subtract the non-needed part of the url?


5 Answers 5


There's nothing built in to return the bit you want but it should be as easy as using the home_url() function and removing it's output from the full url eg:

function get_relative_permalink( $url ) {
    return str_replace( home_url(), "", $url );
  • I came to the same bit of code but was hoping for some function already in core that returns it the way I want.
    – Scott
    Commented Nov 2, 2011 at 17:29
  • 1
    That will result in maybe unexpected values for multisite or with WordPress installed in sub directory.
    – David
    Commented Sep 1, 2016 at 14:50

There's actually a core function for this now. wp_make_link_relative($url)

Convert full URL paths to relative paths.

Removes the http or https protocols and the domain. Keeps the path '/' at the beginning, so it isn't a true relative link, but from the web root base.


<?php echo wp_make_link_relative('http://localhost/wp_test/sample-page/'); ?>

This will output /wp_test/sample-page/

Example with Post ID <?php echo wp_make_link_relative(get_permalink( $post->ID )); ?>

Example for current post <?php echo wp_make_link_relative(get_permalink()); ?>

More about this can be found in the documentation.

  • 2
    This answer should be marked as the correct one, because it uses a built-in helper function of WordPress Core: developer.wordpress.org/reference/functions/…
    – sun
    Commented Jan 22, 2018 at 18:06
  • not the best decision, because if you are started your WP in localhost via MAMP and you have multiple projects on localhost/*, the answer to wp_make_link_relative(get_permalink()) will be /wp_dir/category_slug
    – Gediminas
    Commented Jun 16, 2019 at 10:32

You won't be able to use get_permalink() for that.

If you dig into the code for that function in /wp-includes/link-template.php you'll see why. After the permalink structure is parsed and prepared, the code does this:

$permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );

This is performed immediately after the structure of the link is created and before anything is passed through a useful filter.

So unfortunately, you'll have to extract the non-needed part of the URL yourself. I'd recommend using the str_replace() function that @sanchothefat suggested.


$path = parse_url(get_permalink(...), PHP_URL_PATH); ... gives the URL PATH only. This is not relative to blog root but to domain. It's the absolute URI.

$relative_permalink = wp_make_link_relative(get_permalink($post->ID));

wp_make_link_relative( string $link )

Removes the http or https protocols and the domain. Keeps the path ‘/’ at the beginning, so it isn’t a true relative link, but from the web root base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.