1

I'm trying to figure out how to echo a variable inside a variable.

This code below doesn't obviously work because I'm not echoing the variable

$tweet = get_field('tweet_msg'); //this is getting the string inputted by user in the custom field
$tweet_intent = '<div><a href="https://twitter.com/intent/tweet?text="'.$tweet.'">TEST</a> </div>';

but when ever I do PHP throws an error saying unexpected echo:

$tweet_intent = '<div style="margin-bottom:15px;"><a href="https://twitter.com/intent/tweet?text="'.echo $tweet.'">TEST</a> </div>';

Full code:

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
  $tweet = get_field('tweet_msg');
  $tweet_intent = '<div style="margin-bottom:15px;"><a href="https://twitter.com/intent/tweet?text="'.$tweet.'">TEST</a> </div>';

  if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $tweet_intent, 2, $content );
  }

  return $content;
}

// Parent Function that makes the magic happen

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  $closing_p = '</p>';
  $paragraphs = explode( $closing_p, $content );
  foreach ($paragraphs as $index => $paragraph) {

    if ( trim( $paragraph ) ) {        $paragraphs[$index] .= $closing_p;
    }
    if ( $paragraph_id == $index + 1 ) {
      $paragraphs[$index] .= $insertion;
    }
  }     return implode( '', $paragraphs );
}
Uriahs Victor
  • 1,049
  • 14
  • 32
  • 1
    [*"This is not a duplicate."*](http://stackoverflow.com/revisions/35465691/1) - Oh, how so? It's pretty obvious here. You can't put an echo in like that. Place it befofre you've declared the variable. – Funk Forty Niner Feb 17 '16 at 19:16
  • you can't assign `echo` to a variable. simple remove it – fusion3k Feb 17 '16 at 19:17
  • You say “This code below doesn't obviously work” is the **correct code**. Maybe `$tweet` is empty or is not a string – fusion3k Feb 17 '16 at 19:18
  • @Fred-ii- I said that because i've checked out a number of other posts for this and some were being closed as duplicates when that wasnt even what the author was asking! – Uriahs Victor Feb 17 '16 at 19:18
  • 5
    This guy/girl is probably just starting out and we should be nicer to him/her. – Leo Galleguillos Feb 17 '16 at 19:19
  • @fusion3k it does have a string in it. $tweet = get_field('tweet_msg'); passing the string to $tweet. – Uriahs Victor Feb 17 '16 at 19:19
  • Here `echo $tweet_intent = '';` - *problem solved*. It's valid syntax. or echo the function since you have a return for it. – Funk Forty Niner Feb 17 '16 at 19:19
  • Wow @Fred-ii- cranky much? Can you please remove what you did and add take a look at the code...it's not solved... – Uriahs Victor Feb 17 '16 at 19:24
  • @thebestusernameeverThank you, it's not that I'm "just starting out" It's because this mod just jumped to conclusions. I included full code which clearly shows why I need to do what I'm doing. – Uriahs Victor Feb 17 '16 at 19:26
  • 2
    @UriahsVictor Please pay attention: if the code by Fred doesn't work, then **your `$tweet` variable is empty.** No alternatives. Try to echo it before to see what is inside! – fusion3k Feb 17 '16 at 19:36
  • I did all that before I came here to post, the $tweet variable echos out exactly what was entered by user, I need to concat what was entered by user onto the tweet intent then do some work with that variable. I know trigger-finger-closing-topic mods like @Fred-ii- exist so I did my share of searching around before I decided to post! – Uriahs Victor Feb 17 '16 at 19:40
  • just like @fusion3k said. Plus, we don't know if your code worked in the first place without the echo in there. You need to modify your question to tell us what works or not. Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Feb 17 '16 at 19:45
  • I have posted the full code, I thought you're a PHP guru, can't you see what's going on? I'm getting data entered by the user and i'm concating it into html which is then...nvm I've posted in the appropriate forum. It's clear that you don't see what I'm trying to achieve...of course I have error reporting on. – Uriahs Victor Feb 17 '16 at 19:51
  • I've reopened your question. @UriahsVictor next time, do include more information and pertinent to what it is you want to do here. Like, we're supposed to guess. I mean c'mon. We weren't all born with code built into our DNA. – Funk Forty Niner Feb 17 '16 at 19:54

1 Answers1

5

The problem reside in your <a href> syntax.
Assuming that, after get_field(), the value of $tweet is 'Hello-World', your code:

$tweet_intent = '<div style="margin-bottom:15px;"><a href="https://twitter.com/intent/tweet?text="'.$tweet.'">TEST</a> </div>';

put in $tweet_intent this string:

(...)<a href="https://twitter.com/intent/tweet?text="Hello-World">TEST</a> </div>
             └──────────────────────────────────────┘     

As you can see, the quotation marks of href are closed before $tweet output.

You have to change your code in this way:

$tweet = get_field( 'tweet_msg' );
$tweet = rawurlencode( $tweet ); // only if encoding is not performed by get_field 
$tweet_intent = '
    <div style="margin-bottom:15px;">
         <a href="https://twitter.com/intent/tweet?text='.$tweet.'">TEST</a>
    </div>';
fusion3k
  • 11,568
  • 4
  • 25
  • 47