I am trying to use jQuery to display the comment-section of my WordPress-pages, but without requiring that jQuery be present on all pages that allows comments. Basically, I need the following:
- A generic javascript implementation of the ‘in view’ jQuery-plugin (http://remysharp.com/2009/01/26/element-in-view-event-plugin/)
- A method of activating jQuery (core) when the div containing the comments come into view.
This would then load jQuery, which in turn would load the comments-section of the page, but only when that section became visible through the browser viewport.
The problem seems to be that I really cannot use ‘wp_enqueue_script’ (WordPress’ generic way of adding scripts) for this, as it is a PHP-function. Is there some method that would allow me to implement a functionality as described above (without breaking WordPress/jQuery-functionality)?
EDIT:
I need to enable jQuery only when the reader decides he wants to read comments (as opposed to only opening a page, seeing the title and leaving) – much in the style of Disqus. Disqus appears to be activated only when visible in the viewport, and I am assuming, at the same time the controlling Javascript is activated.
How would I do something like that in regular Javascript (activating jQuery), and then porting it to WordPress?
1. Register jQuery and the plugin within your head
2. Register a meta box
It should be a simple checkbox. Please do it like it’s described on the codex page for add_meta_box() (don’t just repeat this one here). If you follow the rest, then the $id for the meta box should be
'jquery_comments'
.Example (shortened):
inside
functions.php
3. enqueue the script based on your meta box as the condition
and then write a function like this in your
functions.php
fileAfter i understood what your Q is about, i got another A for you:
–> google for “never ending pages” javascript scripts.
Some example:
http://plugins.jquery.com/plugin-tags/neverending
http://plugins.jquery.com/project/in-viewport-event
http://plugins.jquery.com/project/infiniScroll
After a LOT of googling, trying to find a solution at the same time as keeping up with the earthquake-situation in Japan, I found a way:
Two scripts for WordPress
Using wp_enqueue_script, I added a DeferJS (https://github.com/BorisMoore/DeferJS), which allows scripts to be loaded onEvent. Also, a I added a custom-script that looks like this:
Which basically does a quick check every time the viewer scrolls the page, to see if the target element (“comments”) is reached. When it is in fact visible in the viewport, DeferJS loads jquery-1.5.1.min.js (and also drops a quick line into the console.log for verification). Also, when it became visible, the effect from scrolling was removed.
At this point, jQuery will be loaded and ready to use. This method does not, however, allow for use of the wp_enqueue_script-function to load jQuery (but it should be quite possible to extend it to not try to load it if already present). This is because of the obvious separation between PHP and JS.
I owe a lot of thanks to kaiser for his comments and suggestions on the method, so thank you very much for the help.