0

I have an enqueued script called custom_js that loads the file js/custom.js. I want the file to change when a certain condition is true, but it does not seem to work. Here is my code:

add_action('wp_print_scripts', 'my_custom_js', 11);

function my_custom_js()
{
   wp_enqueue_script( 'custom_js', get_template_directory_uri().'/js/custom.js' );

   if ( some condition )
   {
       wp_enqueue_script( 'custom_js', get_template_directory_uri().'/js/custom-2.js');
   }
}

Enqueuing another script seems unnecessary to me; what is the best way to manipulate a script that has been enqueued? Also, it is not very clear to me - do I have to register custom enqueued scripts?

4

The first wp_enqueue_script() in your code should be wp_register_script(). The enqueue function does the job while the register function is getting ready to do the job.

It is always better to rather create a new js file to keep things organised. As to calling them, you can do the following

wp_register_script( 'abc', 'path_to' );
wp_register_script( 'xyz', 'path_to' );

if ( 'condition_a' ) {
    wp_enqueue_script( 'abc' );
} else {
    wp_enqueue_script( 'xyz' );
}

As to whether or not to use the _register_ function, you can use it if you want to. When enqueueing scripts conditionaly, like above, you can use the register function to get the scripts ready for loading if they are called upon by the condition, BUT, it is not necesarry

EDIT

You can also use wp_localize_script() to pass PHP variables according to a certain condition to a js script, and then in your js script you can alter your script according to the value of the PHP condition

  • Isn't there a way to make it simpler? Ideally a single if statement – Aziz May 5 '16 at 8:34
  • How do you mean a single if statement. There is the way in my first code block, and then the other way of using wp_localize_script() to pass the PHP to JS and then doing something in JS according to the PHP passed. I cannot see how one can make this simpler – Pieter Goosen May 5 '16 at 9:02
  • Sorry, I mean without an else statement – Aziz May 5 '16 at 9:06
  • I think that you do not understand PHP. If you need to do something else instead of what you are currently doing, you will need an if/else statement. (or ternary as TheDeadMedic, which is exactly the same thing). Even if you pass a PHP variable to JS script via wp_localize_script(), you will need some sort of if/else statement in PHP and in JS. – Pieter Goosen May 5 '16 at 9:38
  • Not necessarily, I think the following: $var = "script1"; if(something) { $var = "script2"; } is more elegant and minimal. – Aziz May 5 '16 at 9:53
2

Just alter the script URL depending on the condition:

function my_custom_js() {
    $file = some_condition() ? 'custom' : 'custom-2';

    wp_enqueue_script(
        'custom_js',
        get_template_directory_uri() . "/js/$file.js"
        /* array( 'jquery' ) */ /* dependencies, if any */
    );
}

add_action( 'wp_enqueue_scripts', 'my_custom_js' );

You shouldn't be using wp_print_scripts - if you need the script to print after another, use the dependencies argument to specify which scripts yours requires.

  • I did not even realize the wp_print_scripts hook, good spot. A ternary statement is still just an if statement, which the OP does not want. I'm at a point where I do not know what the OP needs exactly, because I cannot see how you are going to get this done without somekind of if/else staement ;-) – Pieter Goosen May 5 '16 at 9:40
  • I think OP wasn't actually opposed to the concept of if statements, just wanted something more concise. – TheDeadMedic May 5 '16 at 9:41
  • @TheDeadMedic I do have dependencies assigned but removed them to keep the question simple - why is wp_enqueue_scripts better than wp_print_scripts? – Aziz May 5 '16 at 10:02
  • 1
    The clue is in the name - enqueue - wp_print_scripts is specifically for at point of printing scripts. – TheDeadMedic May 5 '16 at 10:03
  • All right, thanks for the points. I might just use @PieterGoosen solution as I'm afraid using the same name for enqueue_script might cause conflicts with plugins, especially caching ones. – Aziz May 5 '16 at 10:19

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.