0

For a number of wordpress websites i am creating a cookiewall. So in the plugin file i created a shortcode like this:

function agreebutton($atts, $content = null) {
extract(shortcode_atts(array($atts),$content));
return '<button type="submit" onClick="SetCookie('clickagree','yes')">';
do_shortcode($content) . '</button>'; 
add_shortcode('button', 'agreebutton');

And in a custom template i set a SetCookie function, so if clicked on the button, the cookie would be placed:

<script>
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString(); }</script>

Now the problem is that when i try to return the onClick="SetCookie ('clickagree', 'yes') i get a syntax error unexpected 'clickagree' (T_STRING). Escaping doesnt solve it, and for a shortcode to work you need to return instead of echoing.

Any ideas?

Webdever
  • 485
  • 5
  • 17
  • The sample PHP code you posted is not complete, I guess you made your example too short... But look how your code is rendered here, there's clearly an error with the quotes you're using. Check this Q&A for a solution: [Escaping quotation marks in PHP](https://stackoverflow.com/q/7999148/1287812) – brasofilo May 18 '18 at 02:37
  • You were right, after changing the quotations it did work. Thank you! – Webdever May 18 '18 at 08:51

1 Answers1

0

Ok so after changing the quotations this worked:

return '<button onClick="SetCookie( \'clickagree\',\'yes\')">' . do_shortcode($content) . '</button>';
Webdever
  • 485
  • 5
  • 17