2

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!

1 Answer 1

4

You can check is_single before enqueue the js file

add_action('wp_enqueue_scripts', '_enqueue');

function _enqueue(){
   if(is_single(){
       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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.