I have written a function which outputs a tag in a textarea in which people can link internally to other posts on the website. This works like a charm except with using a postname permalink structure...

For example: people link to a post called 'Hello World' by writing (link: Hello World)... The link to the post, however, is: http://somesite.com/hello-world

So what happens? I get a 404-error because it looks like Hello World does not exist, but it does! It has just no stripe between it and uppercase letters... My question is: how do I make it link to the proper post? This is the code I am using:

 $content = preg_replace('%\(link: ?(.*?) ?\)%', '(link: <a href="http://somesite.com/$postname_test" title="$postname_test">$postname_test</a>)', $content);

1 Answer 1


Spaces are invalid characters in URLs. Your links are broken, whether the posts exist or not and regardless of whether you want the spaces or not.

And uppercase/lowercase can get you in trouble too. Some servers are case sensitive so if you have a permalink to http://somesite.com/hello-world a link like http://somesite.com/Hello-World might work or it might not. You don't want pages to 404 over case, right?

Your code is out of content so it hard to test but ...

$content = "(link: http://example.com/test )"; // assuming links like this
$post= 'Abc Def'; // and assuming $post is the post title

$content = preg_replace(
  '%\(link: ?(.*?) ?\)%', 
  '(link: <a href="http://somesite.com/'.sanitize_title_with_dashes($post).'" title="'.$post.'">'.$post.'</a>)', 

There are a couple of things I noticed about your code.

  1. $post is a global variable used by WordPress. You appear to clobbering it by setting it to the post title instead of leaving it as the post Object as it should be. I would recommend using a different variable name.
  2. In PHP, variables do not expand inside single quotes so the parts of your code where you had variables inside single quotes would not have printed the way you wanted. For example, title="$post">$post</a>)' would have literally been title="$post">$post</a>)'

On further thought, rather than convert the title to the permalink the way you are trying to do you'd be better off just using $post->post_name, with $post being the post Object it should be. WordPress initially converts the title to the permalink the way that I did, but the title can be edited independently of the permalink. There is no guarantee that converting the title will work.

As it turns out the real question is how to capture and convert the user supplied input. For that you need preg_replace_callback.

function transform_pseudo_anchor_wpse_101201($match) {
  if (isset($match[1])) {
    return '(link: <a href="http://example.com/'.sanitize_title_with_dashes($match[1]).'" title="'.$match[1].'">'.$match[1].'</a>)';

$content = preg_replace_callback(
  '%\(link: ?(.*?) ?\)%', 
echo $content; 
  • Yes, I know that it is invalid, but the problem is that the registered users can link to the internal posts, so they are not going to check if it exists or not... If it does not exist; than show a 404 error. But if it exists, but written without the obvious spacing and uppercase, I want it to show me. Commented May 29, 2013 at 21:02
  • I know, I just edited the variable here because it was a very long word... It did not work btw. It just removes the link now. Commented May 29, 2013 at 21:06
  • Your first comment is very, very confusing but if you want to know how to "make it link to the proper post", I just told you.
    – s_ha_dum
    Commented May 29, 2013 at 21:07
  • What is confusing? The goal of the site is to link to posts, whether they exist or not... The problem is that if they write "Hello World" in the tag, they don't get the post on the page /hello-world --> No, they also get a 404 error. That should not happen, because the post exists, but is just written differently (because of the stripe). If it did not exist at all, than this would make sense, but even with sanitize_title_with_dashes it does not seem to work. It removed the whole link now. Commented May 29, 2013 at 21:11
  • "What is confusing?"... well, you want to " link to posts, whether they exist or not". That for one. And the fact that you seem to think that invalid URLs are just "written differently" and should still work. The code I posted works exactly as I pasted it. (You do have to echo it of course.) I have no idea what context you are trying to use this in though.
    – s_ha_dum
    Commented May 29, 2013 at 21:20

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.