Download - Beyond the Theme - Using WordPress as an API
7/30 – 7/31/2011 #wcchicago
Beyond the Theme
Using WordPress as an API
7/30 – 7/31/2011 #wcchicago
About Me
• Find me on online at:@davidscottuftsfacebook.com/davidscottuftslinkedin.com/in/davidtuftsdavidscotttufts.com
• I’m not a WordPress guru, ninja, black-belt, author, blogger, consultant, or hacker
• I do use WordPress everywhere
7/30 – 7/31/2011 #wcchicago
One Year of WordPress at Work• Migrated 14 unique brand sites and over 40 unique domains in 7
languages to WordPress multi-site• Our most popular brand’s average page views increased from
85,000 to 150,000 (over 130,000 daily emails, over 45,000 iPhone/iPad apps, and over 65,000 Facebook fans)
• Our online donations increased by 38%• Reduced server footprint by 40% and our overhead costs
decreased by 30%• Development time, costs, and learning curve for new developers
were also all significantly reduced
7/30 – 7/31/2011 #wcchicago
Goals of a WordPress Based Web Strategy
• Have one install for multiple sites• Allow for a single sign-on across a network of sites• Use an API to allow for content to be easily accessible by
other websites and mobile devices• Integrate web, social, mobile, social bookmarking, and
monetization strategies• Aggregate and curate content from across the network of
sites in one central stream• Think globally—location and language matter
7/30 – 7/31/2011 #wcchicago
Why WordPress
• It met the conditions of our web strategy• It has multi-site capabilities• It is open source (FREE)• It is easy to migrate to• It is easy to use• It is easy to install and upgrade• It is popular (15% of the web runs on WordPress)• It is versatile
7/30 – 7/31/2011 #wcchicago
Versatile you say?
• Extend it with themes and plugins• Modify it with hooks• Create custom post types• Add custom fields• Use custom taxonomies• Specify post formats
7/30 – 7/31/2011 #wcchicago
An Analogy—Part 1
• The WordPress admin area is like a kitchen and you are the chef
• The WordPress plugins are like the kitchen appliances that make for a better cooking experience
• The WordPress theme is like the dining room where the content is laid out for consumption
• There is a one-to-one relationship between your content and its container, the theme
7/30 – 7/31/2011 #wcchicago
Content vs. Container
• A WordPress theme exists only within the context of its container—the web browser
• Your content is consumed from within your website’s theme
• Your RSS feed is the only way in which your content can be consumed from outside your website’s theme
7/30 – 7/31/2011 #wcchicago
How Do People Find Your Site
• Search engines index your content• Searches point people to your site• Users subscribe to your RSS feed• Friends might link to your site• Anyone can share your content on social
networks
7/30 – 7/31/2011 #wcchicago
Your Websites Current Sphere of Influence
7/30 – 7/31/2011 #wcchicago
Introduction to APIs
“An API (Application Programming Interface) is a way for two applications to talk to each other in a common language that both systems understand. An API essentially provides a structured way for applications to get content in a predictable, flexible, and powerful way.”
7/30 – 7/31/2011 #wcchicago
Does WordPress have an API?
• WordPress has XML-RPC for data management at the admin level
• WordPress has multiple built-in APIs that allow developers to enhance WordPress through plugins and themes
• WordPress has a robust query string that enables content filtering through the URL
• WordPress has no end user API that allows for third-party development around your content
7/30 – 7/31/2011 #wcchicago
What Can an API do for You?
• Allows you to have a single instance of your content and users distributed across multiple channels
• Allows developers to build third-party applications around your web-based content or service
• Allows you to enhance the user experience within the context of your site by building an AJAX rich application
7/30 – 7/31/2011 #wcchicago
Separating Content from Container
• WordPress separates the admin and plugins from the theme
• It allows for the content to exist independently of the website’s theme
• Modifying a site’s theme does not impact your content
7/30 – 7/31/2011 #wcchicago
An Analogy—Part 2
• Same kitchen, chef, appliances, and dining room as the earlier analogy
• An API turns your kitchen into a catering business with delivery trucks
• There is a one-to-many relationship between your content and multiple containers: mobile apps, third-party websites, and social networks
7/30 – 7/31/2011 #wcchicago
Adding an API to WordPress Expands Your Sphere of Influence
7/30 – 7/31/2011 #wcchicago
3 Simple Steps to Implementing an API into Your Next WordPress Theme
1. Pass additional query string parameters to the theme
2. Modify the theme to process API calls3. Determine what format to return the
content in
7/30 – 7/31/2011 #wcchicago
Step 1: Pass Additional Query String Parameters to the Theme
• Define a query string parameter to specify whether or not the request being made is an API call. Always default the parameter to false in the theme as to not disrupt non API requests (?api=true)
• Define a query string parameter to specify in what format to return the content (&format=json)
7/30 – 7/31/2011 #wcchicago
Step 2: Modify the Theme to Process API Calls
• Section off the header and footer with conditional statements within files like index.php, single.php, archive.php, category.php, tag.php, and search.php
• Evaluate based on the query string parameters if an API call needs the header and footer
If ( $api ) { // Conditional Header}
If ( $api ) { // Conditional Footer}
// The Loopget_template_part(‘loop’, $f);
<?php $api = $_GET[‘api’]; $f = $_GET[‘format’];
?>
7/30 – 7/31/2011 #wcchicago
Step 3: Determine What Format to Return the Content In
• Create a series of loops in different formats to handle your custom API requests: loop-html5.php, loop-json.php, loop-xml.php
• Allow the get_template_part() function to determine the appropriate format to use:get_template_part(‘loop’, $_GET[‘format’]);
7/30 – 7/31/2011 #wcchicago
Example
• You have built an events management WordPress theme using custom post types for events, sessions, speakers, attendees, and venues.
• Now your conference attendees want to use their smart phones or tablets during an event to manage their schedules, rate sessions, etc.
7/30 – 7/31/2011 #wcchicago
Allowing for Third-Party Development
• Every new “app” is just another instance of a group of your users interacting with your content or service
• Twitter would not be were it is today without an API which allows users and developers to interact with Twitter’s services on their terms
7/30 – 7/31/2011 #wcchicago
Post Rank vs. Page Rank
• The tide is changing from searchability to shareability
• SEO is still be important, but what is more important is how accessible and shareable your content is from multiple devices, apps, and social networks
7/30 – 7/31/2011 #wcchicago
Other API Implementation Ideas
• Use the API calls for AJAX reloading from within the context of the WordPress theme
• Use in conjunction with other query string parameters to determine the post type, posts per page, sort order, etc.
• Be sure to output the correct headers to the browser in the cases where the requested formats are like JSON, XML, or downloadable files
7/30 – 7/31/2011 #wcchicago
Write It Yourself or Use KickPress• The KickPress plugin gives WordPress a public API for content distribution
across multiple platforms and devices• Makes creating and using custom posts types and taxonomies easy• Focuses heavily on the publishing of a wide variety of content types
(videos, books, events, reviews, authors, contacts, etc.)• Enables strong social media integration through a url shortner, social
sharing links, and a social sharing toolbar• Enables content aggregation from across the web into a local news feed• Enables content bookmarking by logged-in users• Extends WordPress through hooks, themes, and plugins, without the need
to modify the core source code and/or database tables
7/30 – 7/31/2011 #wcchicago
WP + API = { }
Beta release coming soon: http://kickpress.org/getting-started/