Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Voltage: Building Static Sites with WordPr...

High Voltage: Building Static Sites with WordPress-Managed Content

We will explore strategies for using WordPress as a collaborative writing room, similar to proprietary alternatives like Prismic.io and Contentful. We will examine techniques for building static sites with Middleman using WordPress-managed content.

Nickolas Kenyeres

June 06, 2015
Tweet

More Decks by Nickolas Kenyeres

Other Decks in Programming

Transcript

  1. Further separation of concerns • Users enter content using a

    GUI • Programmers create templates that utilize the user-entered content • Web pages generated on demand
  2. It’s not • With (pretty much) any CMS: • Wrangling

    together templates and data is complicated • The process is demanding and involves needless work
  3. “The stack” stacks up HTTP Accelerator (Varnish) Web Server (Apache)

    Application Server (WordPress/PHP) Database (MySQL) Object Cache (MemCached) File System
  4. Do you build a solution for your problem or do

    you fit your problem into a solution?
  5. Build a solution • We have the tools: • Static

    site generators • Rapid application frameworks • APIs • CMSs / Writing rooms
  6. Why static sites • Fast • Secure • Scalable •

    Simple themes and integrations • Own conventions, languages, and tools • Excellent version control story
  7. The generators help! Build HTML JS SVG CSS GIF JPG

    PNG Source HTML COFFEE SVG TWIG ERB SCSS GIF JPG PNG JSON YAML MD
  8. More hosting options open up • Shared hosting • VPS

    • GitHub Pages (https://pages.github.com/) • Dropbox (https://pages.github.com/) • Google Drive (https://www.google.com/drive/) • Amazon S3 (http://aws.amazon.com/s3/)
  9. There are drawbacks • Particularly with the concept of keeping

    content in flat files • Hard to contribute (knowledge of Git) • Hard to collaborate
  10. Writing rooms to the rescue • Contentful (https://www.contentful.com/) • Prismic.io

    (https://www.prismic.io/) • Pull in data through the API into a static build process
  11. WordPress as a pure CMS • WordPress can be used

    as an alternative to writing rooms like Prismic.io and Contentful • Content authors use WordPress to manage content and collaborate; not to build web sites • Content pulled out of WordPress and injected into static build process
  12. WP REST API • Popular plugin that will be integrated

    into core • Provides an HTTP REST API • Extensive API for CRUD operations on comments, media, meta data, posts, taxonomies, and users • http://wp-api.org
  13. Advanced Custom Fields Pro • Popular plugin that allows for

    rapid creation of custom fields • Supports many field types, including text, number, image, map, links, and post references • Rich ecosystem of extensions including repeaters • http://www.advancedcustomfields.com
  14. WP REST API Custom Fields • Adds custom fields defined

    with ACF Pro to JSON output • Inserts an object into the meta key with all your fields • http://wordpress.org/plugins/wp-rest-api-custom-fields
  15. Putting the backend together • A 15-minute REST server with

    multi-user support, roles and permissions, and an admin interface 1. Enable the aforementioned plugins 2. Setup friendly permalinks 3. Create custom post types if necessary 4. Setup custom fields if necessary 5. API accessible at /wp-json
  16. Choosing a static site generator • Comes in pretty much

    any language: C, C++, Java, Go, Haskell, Erlang, JavaScript, Ruby, Python, PHP, etc. • Choose a language you a comfortable with and enjoy working with • Choose a system that is mature • Choose a system that lets you work with many different types of inputs, including remote content for maximum flexibility
  17. Key Features • HTML, Markdown, YAML, and JSON format •

    Templating language • Asset pipeline • Live reload • Development server • Common preprocessors and concat/minify out of the box
  18. Useful Extensions • Deploy • Blog • Pagination • SVG

    Fallback • Full list: https://directory.middlemanapp.com/#/extensions/all
  19. Directory Structure • Data • Lib • Source • Images

    • Javascripts • Layouts • Stylesheets • Pages, posts, etc
  20. Source index.html.md /about Build Data /css /js /img layout.html.erb logo.svg

    all.scss _header.scss _footer.scss all.js menu.coffee feed.js _header.html.erb /layouts
  21. Dynamic Files • Data is made available to our templates

    • Useful for creating menus, lists, directories, etc. • Pages can also be dynamically generated from data
  22. Data Source Library In lib/wordpress.rb class WordPress include HTTParty def

    initialize(uri) self.class.base_uri uri end def posts @posts ||= self.class.get(‘/posts?type=post’) end end
  23. Dynamic Pages from Data Source In config.rb @wordpress = WordPress.new(“http://supersecret.dev/wp-json”)

    @wordpress.posts.each do |post| proxy “/blog/#{post[‘slug’]}/index.html”, “templates/post.html”, locals: { post: post } end
  24. Use Data Source in Templates In source/blog/index.html <% wordpress.posts.each do

    |post| %> <div> <h3><a href=“/blog/<%= post[‘slug’] %>/”><%= post[‘title’] %></a></h3> <div><%= post[‘excerpt’] %></div> </div> <% end %>
  25. Nicer than this! <?php
 if (have_posts()) {
 while ( have_posts())

    { 
 the_post();
 ?>
 <div>
 <h3><a href=“<?php the_permalink(); ?>”><?php the_title(); ?></a></h3>
 <div><?php the_excerpt(); ?></div>
 </div>
 <?php 
 }
 }
 ?>
  26. There’s a plugin for that • Hookpress • Turns internal

    hooks into web hooks • Recompile your site when content is updated • https://wordpress.org/plugins/hookpress/