Stuck on simple WordPress shortcode

Hello everyone,

I’m trying to add a shortcode to wordpress but I’m not very good with PHP and have an issue. I’ve created the code that looks like this:

function calculatorMapContent() {
    return 'MAP GOES HERE';
}
add_shortcode('calculatormap', 'calculatorMapContent');

But the code that I need to put inside the return has both “” and ‘’ quotations in it because it uses inline javascript. How can I work around this issue? I thought of maybe packaging the code in a file and calling it. Any help would be appreciated.

Example of map code:

<area alt="" coords="169,186,187,192,217,193,232,114,222,80,211,88,200,94,193,98,193,101,179,120,176,120,176,126,172,132,165,146,160,148,160,152" shape="poly" href="javascript:document.getElementById('#northwest-territories').click();setTimeout(function(){ window.scrollBy(0, 300); }, 400);" />

So if you put double quotes around your string, PHP accepts " as an escape sequence. The slash does not get printed in the output (or stored in the string, because a string in memory ends in a null byte, not a quotation mark.

$string = "This string does \"not\" end in the middle.";
echo $string;

NOTE: If you do put double quotes around your string, any 's you want output must be escaped as well (\\).

2 Likes

That worked perfectly. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.