Wednesday 11:50 a.m.–12:20 p.m.

Integrating Django and WordPress can be simple.

Collin Anderson

Audience level:

Intermediate

Description

I found it surprisingly easy to do a simple integration of Django with an existing WordPress blog. I use Django to display content from WordPress’s database. I’ll explain how I integrated them and what made things easy.

Abstract

Who am I?

  • PHP user 2002-2006
  • Django user since 2006 (0.95)
  • Maintain ~20 live Django websites (all are at least 1.6, many started pre-1.0)

What I did

  • Client had an old WordPress blog with a custom theme we built back in the day (yuck!) and a Django website we built.
    • Needed to provide a consistent navigation and style between the two
    • Needed to make it easy to maintain. (one database, common base template, and avoid writing PHP)
  • Decided to use Django templates for the entire public-facing side of the website, but keep WordPress for editing and managing the content of the blog.
  • I could have used the-real-django-wordpress package, but I wanted to see how much work it would take if I started from scratch using ./manage.py inspectdb. I spent a few hours tweaking models until things worked and had Django naming conventions, but in the end it allowed me to have full control of the models.
  • Views are pretty simple: Post.objects.filter(post_type='post', post_status='publish')
  • Needed to write code to parse WordPress “shortcodes” for images embeds.
  • Comments are the only place where Django writes to the database. A fairly basic ModelForm does the job. They are moderated through the WordPress admin.
  • To keep things easy to maintain, I decided to use no WordPress plugins, and write no PHP code (except wp-config.php).
  • Keep PHP/WordPress running using php-fpm, php-mysql, and a few lines of nginx config. WordPress auto-installs security updates.

What makes integrating Django and WordPress easy?

  • It turns out WordPress maintains a well-established easy-to use editing user experience out of the box:
    • WYSIWYG editing
    • autosave, revisions, and rollback
    • media library
    • custom key/value “meta data” extra fields
    • comment moderation
    • etc
  • WordPress has put 13-years of thought into its Post model so I didn’t have to.
  • Easy to “migrate” an existing WordPress blog (just start using the database :).
  • I ended up having a lot more time to focus on making a great front-end and more time to work on other sections of the website.
  • Can shut off WordPress, and the website is still there.
  • Can always ditch WordPress and go django-only in the future if needed.

Disadvantages

  • It’s PHP. It’s two different worlds, but I’m trying to keep php-side customization minimal.
  • I can’t support every possible feature of WordPress (like widgets), though I can add features when I need them.
  • WordPress database can change over time, breaking the website, though has been pretty stable recently.

Crazy idea for the future

  • Copy WordPress admin’s javascript + css and re-implement backend.

A few anecdotes on content vs application (if there’s time)

  • Content works without needing javascript (SEO crawler bots, curl, etc)
  • If it shows up on a search engine result page, it’s probably content.
  • Content parts of websites are much easier to make than application-like pieces.
  • With applications, it’s generally acceptable to require javascript and use a lot of ajax
  • With content, it’s sometimes possible to serve the exact same content to every user, and then cache it as if it’s static content.
    • (though be sure not to cache security tokens!)
    • simple cache validation strategy: any change in the admin clears the entire cache.
    • pure static content doesn’t have a “Vary:” header.
  • In my situation, Django handles the content, and WordPress is the application.