WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this line …

<div class="permalink"><?php the_permalink(); ?></div>

and the result on my page looks like this …


I guess it could also look like this …


However I'd like to have just mysite.com/whatever/post-or-so without the http:// or www in front of it.

What is the best and easiest way to do so?

Don't get me wrong, this has nothing todo with rewriting permalinks or whatsoever. Just a simple echo of the_permalink() on my page that is not handled as link but as normal text. And in this case I would like to get rid of the http or www.

share|improve this question
up vote 2 down vote accepted

use get_permalink instead of the_permalink and manipulate it however you'd like via php.

share|improve this answer

Like @milo suggested you can manipulate the return of get_permalink(). This can easily be done via several php string functions, here in use is str_replace(). If you have the need to remove both http:// and https:// give an array of needles to str_replace().

$permalink = get_permalink();
$find = array( 'http://', 'https://' );
$replace = '';
$output = str_replace( $find, $replace, $permalink );
echo '<p>' . $output . '</p>';

The above isn't taking care of the www(.) part, but the principle should be clear.

Another possibility for manipulation are php PCRE (Perl Compatible Regular Expressions) functions, here used preg_replace().

$permalink = get_permalink();
$find_h = '#^http(s)?://#';
$find_w = '/^www\./';
$replace = '';
$output = preg_replace( $find_h, $replace, $permalink );
$output = preg_replace( $find_w, $replace, $output );
echo '<p>' . $output . '</p>';
share|improve this answer
    $find = 'http://';
    $replace = '';
    $output = str_replace($find,$replace,get_permalink());
    $find = 'https://';
    $output = str_replace($find,$replace,get_permalink());

    echo '<p>' . $output . '</p>';
share|improve this answer
Please add an explanation to your answer: why could that solve the problem? – toscho Dec 26 '13 at 17:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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