Quotation marks magically alter file path in PHP

Inside “the loop” echo get_permalink() or the_permalink() work fine and produce something like http://www.example.com/path-to-post, until I put these calls inside of quotes in the HTML like so.

<p>
  This is normal HTML!
  "<?php the_permalink()?>"
</p>

At which point it magically becomes a site relative url, like just “/path-to-post”.

Read More

I just figured out that I can avoid this problem by putting a space between the quote mark and the php, which works fine for links and stuff, but what’s going on here? Since when is PHP able to read the content outside of php blocks and react to it? And why would this happen anyway?

Related posts

Leave a Reply

2 comments

  1. Q: Since when is PHP able to read the content outside of php blocks and react to it?

    A: For a very long time (think PHP 4) it’s been able to capture the outputted text into a buffer and then read that buffer like a string. See ob_start(). Technically a function could just check if the last character on the buffer is a " and behave differently in that situation.

    Q: Why would this happen anyway?

    A: Are you sure this is happening with the the_permalink() function? There could be a plugin which is attempting to make sure that absolute urls don’t get used in <a href="<?php the_permalink() ?>"> context. Which plugins do you have installed? Maybe that’s even default behaviour and I’m just not seeing it on my quick scan of the_permalink() in wp-includes/link-template.php.

    You could try a few other ways to get around it. The first thing I would try is using &quot; instead of a literal ".

  2. Well, I’m a fool. There was a plugin, called “Absolute Relative Links” no less, which was reformatting the page. Still, strange the way it operates. It seems to look for a quotation mark immediately followed by anything resembling a URL and reformats it. But even a single space is enough to stop it. And I didn’t realize PHP could read stuff on the page outside of itself. Lesson learned.