-1

I know this question has been asked 100 times, and I've googled it and tried multiple solutions, but I'm still getting this error. I removed all the white space, tried to encode it in UTF-8 in notepad ++ and still no success. Below is my code for my plugin, this is the only code used, and its all in 1 file:

<html>
<head>
<script type = "text/javascript">
function randomCoupGen()
{
var RanNum = Math.floor(Math.random() * 6);
if(RanNum == 1)
{
alert("You rolled a 1");
}
else if(RanNum == 2)
{
alert("You rolled a 2.");
}
else
{
alert("You rolled something other than 1 or 2");
}
}
</script></head><body></body></html>
<?php
function GenCoupon(){
echo "<input type = \"button\" onclick = \"randomCoupGen()\">Click here to see if you won!!</input>";
}
add_filter('woocommerce_after_order_notes', 'GenCoupon', 9999999, 2);
?>

Sorry for the sloppy code, I tried to delete every white space I saw to see if it would help. It took the character from 380 to about 330. I also tried to set the priority to make it load last.

This plugin makes the purchasing of products in my checkout tab on my woocommerce site indefinitely try to calculate the price, with no success.

Any ideas would be appreciated.

3
  • what is html use in plugin file without any inside function or class ? I think something wrong written funciton randomCoupGen(). Apr 27, 2016 at 3:51
  • everything from <html> to </html> will be "unexpected output" for good reason as @user5200704 says... the script should be added via wp_enqueue_script function as a js file, or just echoed by adding a function that echoes it to the wp_footer hook,
    – majick
    Apr 27, 2016 at 10:33
  • ok no probs :-)
    – majick
    Jul 4, 2016 at 6:46

1 Answer 1

1

Anything outside the <?php ?> tags will be echoed as output since it has no conditions.

The fastest way is to wrap in a function and output in the footer.

add_action('wp_footer','custom_random_script');
function custom_random_script() {
    echo '<script>
    ......SCRIPT CONTENTS......
    </script>';
}

There is nothing technically wrong with outputting the script inside your existing function either, it will still work.

function GenCoupon(){
    echo "<input type = \"button\" onclick = \"randomCoupGen()\">Click here to see if you won!!</input>";
    echo '<script>......SCRIPT CONTENTS......</script>';
}

Or put the script in a separate file and use wp_enqueue_script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.