Title: Loading javascript on chosen page ( conditionally )
Last modified: August 19, 2016

---

# Loading javascript on chosen page ( conditionally )

 *  Resolved [axelya](https://wordpress.org/support/users/axelya/)
 * (@axelya)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/)
 * I am trying to load google map on a page and I created a function loading scripts
   in my function php. It loads the map and OK, but I want to limit this function
   being loaded to a certian page.
 * Conditional tags do not work for me here or I don’t use them properly
 * here is the code from function file
 *     ```
       function my_map() {
       if (!is_admin()) {
       wp_deregister_script('g_map');
       wp_register_script('g_map', 'http://maps.google.com/maps/api/js?sensor=false', false, '', true);
       wp_enqueue_script('g_map');
   
       wp_enqueue_script('y_map', get_bloginfo('template_url') . '/js/ymap.js', array('g_map'), '1.0', true);
       }
       }
       add_action('init', 'my_map');
       ```
   
 * Please show me the way to do it

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1474994)
 *     ```
       <?php if (is_page('42')) {
            my_map();
       } ?>
       ```
   
 * 42 would be the page id. You can also use the page name and other options. See
   the [codex](http://codex.wordpress.org/Conditional_Tags).
 *  Thread Starter [axelya](https://wordpress.org/support/users/axelya/)
 * (@axelya)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475057)
 * racer x, thank you for the answer! I tried this way, but it doesn’t catch.
    I
   tried to place it in different places and include add_action into condition. 
   Nada. add_action ignores conditional tag and loads it on any page.
 * If this way suppose to work, then I am missing something here. Otherwise, need
   another approach.
 *  [Safirul Alredha](https://wordpress.org/support/users/zeo/)
 * (@zeo)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475062)
 *     ```
       ...
       function my_map() {
         if (!is_admin() && is_page('42')) {
       ...
       ```
   
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475069)
 * My example was a call to the my_map() function within the theme files whether
   it was index, sidebar, whatever. The function itself could remain in the functions
   file where it belongs.
 * Zeo’s idea will work too, but you need to call this function somewhere if the
   add_action is not working for you.
 *  Thread Starter [axelya](https://wordpress.org/support/users/axelya/)
 * (@axelya)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475070)
 * Thank you guys ! I have tried Zeo’s way before posting my question. It doesn’t
   load scripts.
 * I solved this by **replacing** add_action(‘**init**‘,’my_map’) with
    add_action(‘**
   wp_print_scripts**‘, ‘my_map’)
 * It loads scripts only on chosen page in footer.
 * As I am new to all this stuff and I am not a PHP-guy, I am not sure it is proper
   use of actions and all. I need to learn more about wp_print_scripts
 *  [Safirul Alredha](https://wordpress.org/support/users/zeo/)
 * (@zeo)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475079)
 * Oops I missed the init part. Sorry 😉
 * see: [http://wordpress.org/support/topic/380226](http://wordpress.org/support/topic/380226)
 *  Thread Starter [axelya](https://wordpress.org/support/users/axelya/)
 * (@axelya)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475083)
 * Zeo, thanks for the link! I can see that I did it right.
    I might say – resolved

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Loading javascript on chosen page ( conditionally )’ is closed to new
replies.

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [wp_enqueue_script](https://wordpress.org/support/topic-tag/wp_enqueue_script/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [axelya](https://wordpress.org/support/users/axelya/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/loading-javascript-on-chosen-page-conditionally/#post-1475083)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
