I wanted to use Three.js JavaScript 3D library on my WordPress site, so I tried the three.min.js in the:
-
body of a post
<script src="/three.min.js"></script>
-
then the footer
<script type='text/javascript' src='/three.min.js'></script>
-
and now the Pinboard: Theme Functions (functions.php) file
function pinboard_enqueue_scripts() {
wp_register_script('three.min', '/three.min.js', true);
wp_enqueue_script( 'three.min' );
}
add_action( 'wp_enqueue_scripts', 'pinboard_enqueue_scripts' );
Unfortunately the cube from Mr.doobâs page is not showing up on my Cube page.
Your help in running the Three.js on my WordPress site would be greatly appreciated.
Thank you.
Well that has also a lot to do with CSS ^^ I do the same at http://lizkats.com, maybe get some inspiration there.
In the wordpress theme editor I edited the header file (header.php) to include both the library and the script there. I used an absolute URL (i.e. I cheated, maybe you have more patience and find a better way) and inserted libraries and script like following:
The 3dcontainer is where I insert the canvas object.
Further you’ll also have to update your style.css in order to make it look the way you want. Here’s a paste of my current (entire) stylesheet: http://pastebin.com/9uUiAZhY
Good luck!
This is popular search, so I decided to write little help for feature readers.
I’m assuming that you have properly installed and working WordPress.
Installation without bundlers
The simplest way to make example three.js cube working on your WordPress home page, following three.js docs:
1. Depending on your WordPress configuration open index.php / page.php
2. First we need to import library, there are two common ways to do that via : npm or cdn.
CDN:
NPM (check Downloading and installing Node.js and npm if needed):
Open your terminal and run
npm install three
. This will createnode_modules
folder in your directory, which contains library. Example provided in three.js docs, assumes that you will bundle html. Although we will import our library without bundling for the needs of this question.Open functions.php and enqueue library:
Make a notice, it’s placed in
<head>
so it will be run before our script.3. Add the basic cube to the script:
for NPM type=”module” and
import * as THREE from 'https://cdn.skypack.dev/three@0.137.5';
is not needed. Because we have imported it already using UMD.I might update it in the future for Webpack bundler integration.