Javascript into WordPress
-
Okay I have a google map javascript code I want to insert into my homepage (I also have seperate maps for each of my other pages I will need to insert).
I am struggling with this and nothing is working for me. I have my java file in my host server as map_script.js
First, with this should it just be my java code? or should it have the <script>Insert Java code here</script>? At the end of the code is the code with my API key, how do I add that to my java code? or does that just get added to my homepage html?Second, where do I go from here? I have read the wordpress page with enqueue and tried this, but it just doesnt seem to work for me. Does anyone have a better resource as to how to do this enqueue process or can explain it to me better?
-
Hi there plet9090!
Are you following a tutorial, and if so which one?
Now, for the first question you have:
First, with this should it just be my java code? or should it have the <script>Insert Java code here</script>? At the end of the code is the code with my API key, how do I add that to my java code? or does that just get added to my homepage html?
There are several ways to load JavaScript files within WordPresss. As you may already know, enqueueing them is a better practice. With that in mind, when you do enqueue them WordPress will create the
<script src/>in the header for you. From there all you would need to edit are the respective JavaScript files. For example, if I’m loading my JS files like:add_action( 'wp_enqueue_scripts', function(){ wp_enqueue_script( 'my-cool-js-file', get_template_directory_uri() . '/js/app.js', array( 'jquery', 'underscore', 'backbone' ) ); });The file I would then edit would be the
app.jsfile.Now, if I wanted to add a simple two simple lines of JS I could use something like:
add_action( 'wp_footer', function(){ echo '<script>'; // my super awesome JS code echo '</script>'; }), 99 );As for the second question:
Second, where do I go from here? I have read the wordpress page with enqueue and tried this, but it just doesnt seem to work for me. Does anyone have a better resource as to how to do this enqueue process or can explain it to me better?
Are there any errors when you open up your browser’s developer tool console? If the file is not loading it will often give a 404 error on the file.
Any further questions, let us know!
I am trying to follow this one
What I’m wondering is where am I putting this code?
I feel like I need to put the enqueue code into my header.php and what else do I do?
Or does this go into the head of my header.php? <script type=”text/javascript” src=”/scripts/map_script.js”></script>I just don’t understand what to put where to be honest.
Gotcha!
That makes a little more sense now. 🙂
So, the
add_actionparts you would put inside yourfunctions.phpfile of your theme -or child theme- or in the plugin’s file. The<script>is created by WordPress so you don’t have to worry about that. Make sense?Okay kind of.
I posted this code into my functions.php
function university_scripts() { wp_enqueue_script( 'map js', get_template_directory_uri() . '/js/map_script.js'); } add_action( 'wp_enqueue_scripts', 'university_scripts' );Now, when I look at the source code from my homepage, I can see that my javascript file is shown and I can click on it and its there.
So where should I put this code:
<style> html, body {height: 100%; margin: 1; padding: 1; } #map { height: 80%; } </style>and this
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOURAPIKEY&signed_in=true&callback=initMap"></script>And will this be the solution to my map not showing up onto my homepage?
Thanks for your help!
Great!
So the
scriptyou can either hook towp_headusing something like:add_action( 'wp_head', function(){ echo '<script src="path/to/file.js"></script>'; });The
stylecode you can also hook to thewp_headif you want or you can hard-code that in theheadof your page. Entirely up to you on that one. 🙂That may be the solution to showing the map, if you post a link to the page we may be able to further troubleshoot just in case there are a few things that may not be loading and perhaps missed. 🙂
Okay I have this code in my functions.php file
function university_scripts_map() { wp_enqueue_script( 'map js', get_template_directory_uri() . '/js/map_script.js', array( 'jquery', 'underscore', 'backbone' ) ); } add_action( 'wp_enqueue_scripts', 'university_scripts_map' ); add_action( 'wp_head', function(){ echo '<script src="https://maps.googleapis.com/maps/api/js?key=YOURAPIKEY&signed_in=true&callback=initMap"></script>'; });Of course my API key is in there. But still this isn’t working for me. Any ideas on what I am missing? Do I need to input anything into my homepage text to show the map there?
Again thanks for your help, it is greatly appreciated.
Are you able to post a link to the page/site?
From what I can gather it looks like you don’t have any element with an
IDofmap. That is what themap_scriptis looking for in order to create the map.I have tried to post the <div id=”map”></div> code into the body of my page with no luck. Is this what you are talking about? I figured it must also be something with the code with my API key. Is this how to correctly write it like I did in my previous comment in the functions.php?
where should i place the <div id=”map”></div>
and where do i place the css for the map?
Try changing the
wp_headtowp_footer. It could very well be it is rendering sooner than needed.add_shortcode('custom-map-shortcode', 'custom_map_shortcode_callback'); function custom_map_shortcode_callback() { return '<div id="map" style="height: 100%; margin: 0;padding: 0;"></div>'; } add_action( 'wp_enqueue_scripts', 'enqueue_map_script' ); add_action( 'wp_head', function(){ echo '<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAsQtOoS2PUGzaip_GDVzVhxKNI5mLVr1A&signed_in=true&callback=initMap"></script>'; }); function enqueue_map_script(){ global $post; if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-map-shortcode') ) { wp_enqueue_script( 'map-script', get_template_directory_uri() . '/js/map_script.js'); } }[Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]
the footer change didnt work. I have this code so far, and I know I’m almost there because the js path file is in my source code, I also see my script source with my api key. If I put the shortcode into the body of my page, it doesn’t show up in the page, so I know the shortcode is working. I just can’t find what else I am doing wrong.
The topic ‘Javascript into WordPress’ is closed to new replies.