Webdever
Webdever

Reputation: 525

return html button with onclick cookie value

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?

Upvotes: 0

Views: 63

Answers (1)

Webdever
Webdever

Reputation: 525

Ok so after changing the quotations this worked:

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

Upvotes: 0

Related Questions