0

I'm trying to insert a Google Chart (directly, from HTML, rather than using the Publish option on a Chart from within Sheets. Sheet charts don't do what I need) directly into the WordPress web interface.

Unfortunately, the <script> has to be inserted in head to work. Any ideas?

I aim to have a unique chart on every page/post (it's a blog about star mapping, and I'm using a scatter plot as the X/Y star locations), so doing it in a template won't work.

4
  • 3
    Can you show us what you've tried so far? It might help you get a response quicker. Commented Mar 31, 2018 at 1:21
  • Sure! Bear in mind I'm a relative code novice. I know my way around HTML, but I'm only using the web interface for Wordpress and have been using it for less than a week now. The code I'm using for star mapping is pretty simple, it's just the script for Google Charts. (it's too long to post here) When pasted into a HTML, the code draws the chart correctly. However, when I try to paste the script into Wordpress, I get actual code showing up, presumably because I can't write it to head. Commented Mar 31, 2018 at 18:00
  • What other info could I supply that would help answer this? The character limit is too small for me to paste the actual code I’m attempting to use. Commented Apr 1, 2018 at 15:28
  • Here's the thing: No one will read that amount of code. You need to narrow it down to just a few lines of problematic code. Error messages usually help provide a way to focus on the important bits. It might be less an issue with code, and more of an app -specific/UI problem. I'm not a WordPress user, but I'm sure there are many on this site. Commented Apr 1, 2018 at 15:50

1 Answer 1

1

I am not able to understand your whole question properly. But as you mentioned, you want to add script to head then you can use below code in your theme's functions.php

    function hook_javascript() {
        ?>
            <script>
                alert('Page is loading...');
            </script>
        <?php
    }
    add_action('wp_head', 'hook_javascript');
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for replying! If I'm using the web interface for Wordpress and I want to write a unique script in each post's head, would this work? Where would I go to do that. Again, apologies - code novice.
Yes, it will work. If you have java script code then use above method. If you have .js file then use wp_enqueue_script() You need to do this change in your theme's functions.php
Excellent, thanks! Can you walk me though what your hook does so I can be sure I’m using it right?
Above code will add javasript code/file to your page. You can see in view page source.

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.