1

Hope you're all well.

So here's what I want to do. I want to add to a review plugin in wordpress the possibility to open the page I want in a new window with the target="_blank" code.

I believe that's where the magic is happening, this is the original:

if ($show_morelink != '') {
                $review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."'>$show_morelink</a>";
            }

This is what I did without any success:

if ($show_morelink != '') {
                $review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target="_blank">$show_morelink</a>";
            }

I'm a beginner in PHP and I hope that someone can help me with this... I know it's not so hard.. I'm just missing something.

Thanks!

ZaX
  • 15
  • 6

5 Answers5

3

You must escape your quotes.

Use the following

    $_morelink != '') {
        $review->review_text .= "<a href='".$this->get_jumplink_for_review($review,1)."' target=\"_blank\">$show_morelink</a>";
    }

Source for handling strings.

emsoff
  • 1,585
  • 2
  • 11
  • 16
1

Because your code is surrounded with double quotes, you are breaking out of them when you add in the target. You can either escape the quotes like this using a slash:

$review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target=\"_blank\">$show_morelink</a>";

Or change to using single quotes:

$review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target='_blank'>$show_morelink</a>";

Edit A third way you could do it is surrounding the whole string in single quotes and remove the single quotes and periods form inside:

$review->review_text .= ' <a href="$this->get_jumplink_for_review($review,1)" target="_blank">$show_morelink</a>';
stckrboy
  • 379
  • 10
  • 16
0

Your problem is that you're using doublequotes to denote php strings, so you can't use doublequotes for your html:

if ($show_morelink != '') {
    $review->review_text .= " <a href='".$this->get_jumplink_for_review($review,1)."' target='_blank'>$show_morelink</a>";
}

If you look at the HTML output you will see that both the href and target use single quotes now.

Digital Chris
  • 6,177
  • 1
  • 20
  • 29
0

I always prefer to use single quotes for HTML code strings to improve readability.

if ($show_morelink != '') {
    $review->review_text .= '
      <a href="'.$this->get_jumplink_for_review($review,1).'" target="_blank">'.$show_morelink.'</a>';
}
Aycan Yaşıt
  • 2,106
  • 4
  • 34
  • 40
  • this will not work.. `'$show_morelink'` will be literally `$show_morelink` instead of the value of that variable – ddavison Jan 30 '14 at 16:58
  • There is actually a technical difference between single and double quotes in PHP - one is a string literal the other isn't. Arguably I'd say it's better practice to use them in the way they're intended - literals for literals (keys in associative arrays and the like) and double-quotes when you need to interpolate strings with variables (outputting HTML for instance) - even if that means escaping your output strings. – CD001 Jan 30 '14 at 17:03
  • sorry - i deleted my comment because i thought this comment was below on another answer that I commented on `;)` yes, you're right. it's typically bad practice to wrap strings in literal unless you really want literal. – ddavison Jan 30 '14 at 17:08
0

Lots of answers; most correctly pointing out the incorrect escaping of the quotations.

As it has not been mentioned yet sprintf() can also help with readability rather than having to concatenate strings.

$link = $this->get_jumplink_for_review($review,1);
$text = sprintf('<a href="%s" target="_blank">%s</a>', $link, $label);
AlexP
  • 9,906
  • 1
  • 24
  • 43