D3.js Example

Making D3.js and WordPress Play Nice

WordPress is a great, flexible blogging platform, but it can make things that are easy in straight HTML, like simple Javascript, kind of a pain. Slightly more complicated things, like showing demos of neat D3.js tricks, can be tough.  This afternoon, though, I got it working.

UPDATE July 2016 – while the steps below worked for me originally, this has either gotten a whole lot easier, or I was making it too complicated in the first place.  You should be able to just do step #5 (install Raw HTML), then wrap all your javascript in the raw tag.  Think I’ll leave this here for historical purposes, though.


D3.js Example
A shot from some sample code for learning D3.js.

If you want to incorporate D3 on some, but not all, of your pages in WordPress, this seems to be the way:

  1. Add a custom page template to your theme. It’s pretty straightforward – copy pages.php, then rename it. Give it a name that you’ll recognize when you make a new D3 sample page by adding the following line to the comments at the top of the page: “Template Name: D3 Page Template”. You can put whatever you want after “Template Name”, but this seemed pretty unambiguous to me.
  2. Include D3 in your new template by adding the following line after the initial comments, but before “get_header()”: “wp_enqueue_script(‘d3-js’, ‘//d3js.org/d3.v3.min.js’);”. WordPress uses wp_enqueue_script for adding javascript libraries. You can reference a local rather than hosted copy of the library, but that’s a bit more complicated; using the hosted version got me up & running faster.
  3. Edit your new page template to have an element that will hold your D3 example. I created a “graph” element, but you could use a div with a specific id; it doesn’t matter. You probably want to put the element either just before or just after the line with ““.
  4. Upload your template (perhaps goes without saying).
  5. Install the Raw HTML plugin for your WordPress install. If you don’t WordPress will insist on wrapping your javascript code in paragraph tags (even if you use the “Text” rather than “Visual” tab for writing your pages), and nothing will work.
  6. Make a new page using the template you uploaded. To add a D3 code snippet to the page, follow the plugin instructions for wrapping your code in “raw” tags. Make sure that your code appends your SVG (or whatever) element that D3 will be working on to the empty element you created in the template.

That’s about it. Seems simple, but took me hours to figure out. Look for more D3 examples to come…

2 thoughts on “Making D3.js and WordPress Play Nice”

  • Hi,

    I would like to ask you a question about step 3 of your post. It is not very clear where to create this new “graph” element and what to do next.
    For example part of my pages.php follows:

    wp_enqueue_script(‘d3-js’, ‘//d3js.org/d3.v3.min.js’);

    <div id="main-wrapper" class="”>

    <div class="”>

    …………………………………..

    do I have this new element in the right place in my pages.php file?

    • Hi,

      I would like to ask you a question about step 3 of your post. It is not very clear where to create this new “graph” element and what to do next.
      For example part of my pages.php follows:

      …..

      <div class="”>

      …………………………………..

      do I have this new element in the right place in my pages.php file?

Leave a Reply to p_ebox Cancel reply

Your email address will not be published.