0

I converted a jQuery plugin into a Wordpress one where I reference the method is_single():

if (is_single()) {
    // Do something
}

BTW, this code is in my js file. Is that the mistake I'm making?

The plugin installs and runs correctly, but, I get an error saying is_single can't be found. How do I fix this? What am I doing wrong exactly?

2
  • 2
    show us your code but is_single() is a WP php function and not a JS function........ there are ways to use php in a JS file though but depends on what your goal is. – Gareth Gillman Nov 18 '16 at 11:01
  • That's it. That's my problem. I thought it was a JS function. Can you direct me how to do it then? – darkhorse Nov 18 '16 at 12:13
0

The easiest way in WP to write php into JS is using wp_footer

function sse246626_js() {
 if(is_single()) {
  echo '<script type="text/javascript">';
   echo 'jQuery(document).ready(function($) {';
    echo '$("#mydiv").hide();';
   echo '});';
  echo '</script>';
 }
}
add_action('wp_footer', 'sse246626_js');

This way gives you access to the WP functions as you need, there are probably better ways of doing it if you have lots of JS (php file as a JS file) but it gets slightly complicated as you need to load the WP functions to be used in the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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