WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I want to run the javascript function launchMyPlugin() only on post detail pages. At first, I was doing something like this:

myPlugin.js

if ( is_single() ) {
    launchMyPlugin();
}

However, this obviously does not work since is_single() is a php function. What is the right way to do this then? Do I need to add the condition to my .php file instead? Thanks!

share|improve this question
up vote 4 down vote accepted

You can check is_single before enqueue the js file

add_action('wp_enqueue_scripts', '_enqueue');

function _enqueue(){
   if(is_single(){
       wp_enqueue_script(......);
   }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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