Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Yalcin Arsan
Yalcin Arsan
5,149 Points

Insert javascript into WP page

What is the best way to insert a piece of script into a typical wp page?

I tried to paste it into html view of the editor but this has varying results (I believe) depending on the theme used. ie: I followed and completed Bootstrap to WP course and tried it on the finished example but it does not seem work..

Any ideas?

10 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

You can also use the Advanced Custom Fields plugin to add a new text box field to paste in the code then output on the front end without anything being stripped.

Yalcin Arsan
Yalcin Arsan
5,149 Points

Gareth, thank you for your comments. That was fast!..

It is a "google charts script" that displays a dynamic chart created by google charts using live data taken from a google e-sheet (see code below)

FYI: Please see the link that has many of these charts that I use currently on an HTML template: www.otomotivkarnesi.com

<script type="text/javascript" src="//ajax.googleapis.com/ajax/static/modules/gviz/1.0/chart.js"> {"dataSourceUrl":"//docs.google.com/spreadsheet/tq?key=0ApgJZsyOJSD8dG91c05OWjBkdnlLaXh1X2lraXNyeFE&transpose=0&headers=1&range=Q20%3AQ21&gid=12&pub=1","options":{"greenTo":null,"titleTextStyle":{"fontSize":16},"vAxes":[{"useFormatFromData":true,"title":"Sol dikey eksen ba\u015fl\u0131\u011f\u0131","minValue":null,"viewWindow":{"min":null,"max":null},"maxValue":null},{"useFormatFromData":true,"minValue":null,"viewWindow":{"min":null,"max":null},"maxValue":null}],"booleanRole":"certainty","title":"Grafik ba\u015fl\u0131\u011f\u0131","max":812126,"animation":{"duration":0},"hAxis":{"useFormatFromData":true,"title":"Yatay eksen ba\u015fl\u0131\u011f\u0131","minValue":null,"viewWindow":{"min":null,"max":null},"maxValue":null},"width":218,"height":218},"state":{},"view":{},"isDefaultVisualization":true,"chartType":"Gauge","chartName":"Grafik 37"} </script>

Gareth Borcherds
Gareth Borcherds
9,372 Points

If you are going to be putting these charts on lots of pages and have a different chart for each page, I would recommend using custom meta boxes. A good plugin that'll help you get started is probably http://wordpress.org/plugins/meta-box/

but if you want to read up on them and how they work, you can start here in the Wordpress documentation. http://codex.wordpress.org/Function_Reference/add_meta_box

Basically I would add a custom meta box to your posts that is specifically for these charts' code and then integrate the output of this box with your theme. Good luck!

Gareth Borcherds
Gareth Borcherds
9,372 Points

There are a variety of ways that you can do this, it really depends on what the purpose of it is. If it's something like a google analytics script that needs to go on every page, I typically put it in the footer template page.

If it's something specific to a page I've done everything to coding a specific inclusion in the them functions.php file to putting an if statement in the page template to include it there.

I've also created custom meta boxes where I can out specific lines of js and have them show up with a page or post. It just depends on what makes sense for the problem you are trying to solve and if a simple hack will suffice or if you need a more elegant solution.

If you share some more details of what the JS code is, we might suggest a better approach, or maybe debug why it doesn't work by putting it in the editor as that has worked for me on a variety of js outputs and maybe there is another issue preventing it from working?

Just some thoughts. Maybe someone else can provide an even better solution.

Gareth Borcherds
Gareth Borcherds
9,372 Points

Oh yeah, that's the plugin I was looking for when I suggested the meta-box plugin, the Advanced Custom Fields plugin is the coolest plugin out there. My development partner uses that on almost every custom wordpress site he builds.

Yalcin Arsan
Yalcin Arsan
5,149 Points

I believe that is one of the plugins that Zac had us install during the Bootstrap-to-WP project. I thought about using it previously but could not figure out how to use it to store a script to display on a page section?..

I will give this some more thought now.

Thanks, I'll be in touch :)

Yalcin Arsan
Yalcin Arsan
5,149 Points

Zac, I'm trying to get my head around how to make use of Custom Fields for my purpose.

Before I go further I have one question: Why would a script that would not function/display properly when inserted in the html section of the editor, work correctly when inserted in a custom field?..

Yalcin Arsan
Yalcin Arsan
5,149 Points

Zac - either I am doing something wrong, or the ACF custom field is not "running" the script, only displaying the text. Please see a trial in the link:

http://otomotivkarnesi.com/yeni/

Gareth Borcherds
Gareth Borcherds
9,372 Points

Why don't you try installing this addition to the plugin to give you a field that seems ready to do what you're looking for.

https://wordpress.org/plugins/advanced-custom-fields-code-area-field

Yalcin Arsan
Yalcin Arsan
5,149 Points

This does get rid of the code being displayed on the page but still won't output the resulting chart on the page.

It's certainly a step in the right direction though... I'll keep working on this. Thanks!.

Yalcin Arsan
Yalcin Arsan
5,149 Points

Update: With a little effort, concentrated thinking and a few visits back to Zac's workshop, it finally turned out nicely for my html to wp project: http://otomotivkarnesi.com/yeni/ .

Two notes: The ACF plugin was the key to solve my problems. However the more I dug into it, the more I kept trying to use it advanced features (which obviously were too advanced for me for now) before I finally gave up, ending up using the basic functionality as described in the workshop.

  1. However I believe that ACF is a fantastic plug-in that deserves a course and/or a workshop on its own. Especially used with the two add-ons: The "repeater field" and the "flexible content field" you can almost achieve any type of layout. I am pretty sure these would have helped me realize my project in a much shorter / effective way, but my level of php + wp expertise was simply not enough to use these effectively. I could not manage to create the correct loops & queries to use these add-on plugins in new page templates.

Anyway, it was a very valuable experience. Thanks!.