Return a string that has a variable inside a WordPress Shortcode

+1 vote
asked Sep 12, 2017 by somethingnow

If I have this →

<input type="hidden" name="meta_adtracking" value="custom form" />

then I can easily convert this into a returnable string →

$output =  '<input type="hidden" name="meta_adtracking" value="custom form" />'

Of course the output will later be returned:

return $output;

But this becomes challenging for me when It has a jQuery or a Wordpress Localized Function.

Wordpress Localized Function

<input type="submit" class="one" name="aweber_submit" value="<?php _e("Subscribe", 'text-domain');?>" />

How to convert the above into a returnable string? [P.S. → The php tags are actually not required because the shortcode function is in a file that is .php extension], but then If I just remove a PHP tag would that be ok?

$output = '<input type="submit" class="one" name="aweber_submit" value="_e("Subscribe", 'text-domain');" />'

Is this Ok?

2 Answers

+1 vote
answered Sep 12, 2017 by kinggs

You will need to change the following to prevent the PHP function being stored as part of the string or in this case, causing a parse error because of the inverted commas.

$output = '<input type="submit" class="one" name="aweber_submit" value="_e("Subscribe", 'text-domain');" />'

to

$output = '<input type="submit" class="one" name="aweber_submit" value="'._e("Subscribe", 'text-domain').'" />';

Also note that the _e function will need to return a value for expected behavior.

+4 votes
answered Sep 12, 2017 by delboy1978uk

Almost. Try this!

You need to break out of the string and concatenate whatever the function returns.

$output = '<input type="submit" class="one" name="aweber_submit" value="'._e("Subscribe", 'text-domain').'" />';
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...