1

A plugin developer who developed a comments-plugin that I use has instructed me to add the following JavaScript:

function WPACLoadMoreComments() {
  window.WPACLoadMoreCount = window.WPACLoadMoreCount || 1;
  var url = (new Uri(location.href)).replaceQueryParam('WPACTake', window.WPACLoadMoreCount * 20).toString();
  if (WPAC.LoadComments(url, {updateUrl: false})) {
    window.WPACLoadMoreCount++;
  }
}

I assume he meant to put it in functions.php but the site doesn't load when I insert this code. I tried to inset it at the end, I tried to wrap it with

<?php

the function...

?> 

How do I do that correctly?

1
  • wrap it in <script>function</script> tag ... Commented Feb 20, 2015 at 9:02

4 Answers 4

1

You need to add the code to a javascript file and enqueue it in functions.php, or echo it via an action hook.

There's a section about including JavaScript right in the codex that's worth a read.

Sign up to request clarification or add additional context in comments.

Comments

0

add below code into your functions.php file

function comment_script(){
$html = "<script type='text/javascript'>
   function WPACLoadMoreComments() {
     window.WPACLoadMoreCount = window.WPACLoadMoreCount || 1;
     var url = (new Uri(location.href)).replaceQueryParam('WPACTake',   window.WPACLoadMoreCount * 20).toString();
  if (WPAC.LoadComments(url, {updateUrl: false})) {
    window.WPACLoadMoreCount++;
  }
}
</script>";
echo $html;
}
add_action('wp_footer','comment_script');

1 Comment

Thanks! it was hard to choose between all the great answers i got, but i simply added your code to the bottom of functions.php and it seems to work :)
0

This is a Javascript function, not a PHP function. This means that you need to do the following:

<?php

// Your existing PHP code here

?>
<script>
  function WPACLoadMoreComments() {
  window.WPACLoadMoreCount = window.WPACLoadMoreCount || 1;
  var url = (new Uri(location.href)).replaceQueryParam('WPACTake', window.WPACLoadMoreCount * 20).toString();
  if (WPAC.LoadComments(url, {updateUrl: false})) {
    window.WPACLoadMoreCount++;
  }
}
</script>
<?php

//Your remaining PHP code

?> 

Another possibility is to do it this way:

<?php

echo "<script>";
echo "  function WPACLoadMoreComments() {";
echo "  window.WPACLoadMoreCount = window.WPACLoadMoreCount || 1;";
echo "var url = (new Uri(location.href)).replaceQueryParam('WPACTake', window.WPACLoadMoreCount * 20).toString();"
echo "  if (WPAC.LoadComments(url, {updateUrl: false})) {";
echo "    window.WPACLoadMoreCount++;";
echo "  }";
echo "}";
echo "</script>";

?> 

The reason we're doing it this way is that Javascript is not executed on the server but on the user's browser (client side). Thus, there is no need to put the Javascript in <?php ?> tags, because you do not want it to be executed as PHP code. Since it will be executed by the browser, this means you need this code to appear in the HTML document loaded by the browser, and hence you should use echo or write it within <script> tags outside the <?php ?>

Performance-wise, it is always better to put Javascript code at the end of your page. This is to make sure that any possible lags, caused by the JS code while a user's browser is loading your page, do not affect the rendering of the page.

1 Comment

This is awful practice, and will probably lead to Headers already sent errors. Better to echo or return it
0

Put it in the functions.php or footer.php file somewhere outside <?php ?> and wrap it into <script type="text/javascript">Your function here...</script>

2 Comments

Adding scripts to footer.php is not the ideal way of adding scripts to themes.
@mevius, can you please give more detail on why this is not ideal? within the link about including JavaScript you gave in your answer. The codex states that (assuming you want the JS on every page) To use JavaScript repeatedly within your site, you can either set the call for the JavaScript, or the script itself, in the head of your header.php template file ....no differently than you would if you were using JavaScript in any HTML page . So then I thought loading from the footer.php would be better as it allows page to render before loading JS?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.