0

I want to add a JavaScript file containing my codes for displaying a Google map application in a WordPress page. In the page I just have a "div" tag which will accommodate the Google Map. How can I link the .js file in the page? Also what will be the best location to place the folder in my server containing the .js file?

Thanks in advance for your answer!

4
  • put your js file in js folder. themes/your-theme/js Commented Jul 11, 2014 at 7:10
  • you can link js file using bloginfo(). Commented Jul 11, 2014 at 7:11
  • What have you tried already? What happened? Did you try searching the web for answers? Commented Jul 16, 2014 at 11:26
  • I tried searching the web, but could not find the solution. I've written the js file for producing the Google Map. Now I want to show that map in a page of my Wordpress site. Can't figure out how to include that js file in the specific page. Commented Jul 16, 2014 at 11:40

2 Answers 2

1

on your sever you will must be having a folder with all the wordpress files. In those files you will find folders wp-content > themes > (current theme you are using on the WordPress). Open this folder containing the theme files. Here create a folder to place all your js files. If a folder for js already exists use that one. Now add your js file in that folder. In the folder containing all your theme files you will find a file 'functions.php'. Open this file and add the following code.

add_action( 'wp_enqueue_scripts', 'maps_load_scripts');
function maps_load_scripts(){
  wp_enqueue_script( 'google-maps-js', get_stylesheet_directory_uri().'/(name-of-folder-  containing-all-js-files)/(name-of-your-js-file).js', array('jquery'), '1.0', false);
} 

This will load your js file on all the pages. If you want to load the js file on a particular page then all the above code in the condition:

add_action('template_redirect', 'load_js_certain_page');
function load_js_certain_page(){
$page_id = ;// Use the id of the page in WordPress here. You can find the page id from the database or the WordPress backend.
if(is_page($page_id)){
//add the code from the block above here. 
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot buddy! I think this is what I was looking for if this works. Should I not use a child theme, and copy the functions.php file and add my js files there?
Yes, using a child theme will be better. Creating a child theme will prevent your changes from being overwritten at the time of updating the theme. For creating a child theme refer codex.wordpress.org/Child_Themes . If you find my answer useful please accept the answer
0

First of all , i like to clarify your question a little bit . Do you want the code to be part of a single page or part of the whole template/layout ?

If you want that to be part of a single page/post please refer the below tutorial: http://www.tipsandtricks-hq.com/how-to-add-javascript-in-a-wordpress-post-or-page-1845

Or else if you need it to be part of the whole layout , you should consider making a widget and make changes to the template to accommodate that in place.For making a widget refer this document. http://codex.wordpress.org/Widgets_API

1 Comment

I want the code to be part of a single page. Checking the tutorial. Thanks!

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.