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

Katarzyna Walsh
Katarzyna Walsh
24,409 Points

How to add javascript snippets to wordpress pages?

Hello, I've been trying to add javascript snippets to my wordpress pages but nothing displays in the browser. Do I need a special plugin? Thank you!

5 Answers

Jonas ร…berg
Jonas ร…berg
9,459 Points

Hi, do you know the Wordpress Theme Hierarchy by heart? In order to add scripts / your own code to pages, simply create the page in php for the wordpress page you have created in Wordpress dashboard. Example: If you created the page "About" in Wordpress dashboard, go to your wordpress theme folder and create a file named "page-about.php".

If you want to link scripts or files directly in your page templates you can use <script src="<?php bloginfo('template_directory'); ?>/path_to_file"></script>

I hope this was helpfun! You should check out these two courses, they will teach you anything you need to know about wordpress:

https://teamtreehouse.com/library/the-wordpress-template-hierarchy https://teamtreehouse.com/library/wordpress-theme-development

Matthew Mariner
Matthew Mariner
17,666 Points

This may be unpopular, but depending on your page editor you can go ahead and just insert a <script> YourJSgoeshere</script> right into the body.

This is by no means best practice, but if your situation calls for a bandaid fix. This is a bandaid fix.

If you plan on using jQuery, Make sure that your header() function in your theme is calling for jQuery.

Jonas ร…berg
Jonas ร…berg
9,459 Points

Are you looking to fetch images with your script by using affiliate links? I would suggest using json http://developer.ebay.com/devzone/finding/concepts/MakingACall.html

Or alternatively create your own products pages, fetching data can be a hassle and it can result in poor customer experience as your website may appear slow.

Katarzyna Walsh
Katarzyna Walsh
24,409 Points

Hi, thank you both for your answers. I'm doing something very simple - adding ebay affiliate links to my product pages. The snippet should automatically work and display an image in the post but it displays nothing so I was wondering if there is a plugin that I need to use. Does this make sense? Or do you think that creating new files is the way to go? Thank you!