Title: Conditional enqueue script (JavaScript)
Last modified: February 26, 2018

---

# Conditional enqueue script (JavaScript)

 *  [deanljbirch](https://wordpress.org/support/users/deanljbirch/)
 * (@deanljbirch)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/conditional-enqueue-script-javascript/)
 * I have 3 plugins all of which require Google Maps API to load up (separate keys
   are used on each plugin too).
 * However these are loading on each page causing errors.
 * Now, I have decided to conditionally enqueue each script like so;
 *     ```
       add_action('wp_enqueue_scripts', 'add_script_function');
       function add_script_function() {
           if (is_page(2393)) {
               wp_enqueue_script('SERVICESGMAPI', 'https://maps.googleapis.com/maps/api/js?key=KEYHIDDEN&libraries=places&language=en-GB');
               );
           }
       }
       add_action('wp_enqueue_scripts', 'add_script_function');
       function add_script_function() {
           if (is_page(vacancies)) {
               wp_enqueue_script('VACANCIESGMAPI', 'https://maps.googleapis.com/maps/api/js?key=KEYHIDDEN&libraries=places&language=en-GB');
               );
           }
       }
       add_action('wp_enqueue_scripts', 'add_script_function');
       function add_script_function() {
           if (is_page(events)) {
               wp_enqueue_script('EVENTSGMAPI', 'https://maps.googleapis.com/maps/api/js?key=KEYHIDDEN&libraries=places&language=en-GB');
               );
           }
       }
       ```
   
 * BUT, with the events page, each event is permalinked like so;
    - mydomain.tld/event/marathon
    - mydomain.tld/event/meeting
    - mydomain.tld/event/EVENTNAME
 * So the code above (3rd part) will load the script on mydomain.tld/events, how
   can I set it to work on the event/* pages?
 * Also, the scripts are being loaded via the plugins, if I change that via editting
   the plugins, changes will revert on updates. Any advice?

Viewing 1 replies (of 1 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 3 months ago](https://wordpress.org/support/topic/conditional-enqueue-script-javascript/#post-10016154)
 * Duplicate of [https://wordpress.org/support/topic/google-maps-api-multiple-times-fix](https://wordpress.org/support/topic/google-maps-api-multiple-times-fix)
 * Please continue there.

Viewing 1 replies (of 1 total)

The topic ‘Conditional enqueue script (JavaScript)’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [maps API](https://wordpress.org/support/topic-tag/maps-api/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * Last activity: [8 years, 3 months ago](https://wordpress.org/support/topic/conditional-enqueue-script-javascript/#post-10016154)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
