Your SlideShare is downloading. ×
WordPress Standardized Loop API
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

WordPress Standardized Loop API


Published on

I gave this presentation at WordCamp Raleigh on May 22, 2011.

I gave this presentation at WordCamp Raleigh on May 22, 2011.

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Standardized Loop API The Next Big Thing?
  • 2. Who Am I?
      • Chris Jean
      • @chrisjean
      • Developer for iThemes
      • Core contributor
      • Person who wants to make WordPress better for everyone
  • 3. The Problem
      • There is very little in the way of methods for plugins to interact with a theme at The Loop level.
      • Brief history of integration strategies:
        • Plugin readme.txt files filled with instructions on integrating function and action calls into theme template files.
        • Plugin-provided or custom built page template files.
        • Use of shortcodes as injection points for plugin-generated content.
        • Direct theme and plugin developer discussions to create specific solutions.
      • These solutions simply aren't sustainable.
  • 4. The Goal
      • To make it easier for themes to provide a foundation of style that plugins can make use of
      • To make it easier for plugins to provide output that themes can style
      • To do this in a way that does not require the theme to know anything about the plugin and does not require the plugin to know anything about the theme
  • 5. Case Study: BuddyPress
      • BuddyPress has a great need to interact with themes at The Loop level.
      • The BuddyPress solution was to build a theme completely tailored specifically for the plugin.
      • The end result is that it is extremely difficult to build a BuddyPress-compatible theme without building a child theme of the BuddyPress Default theme.
  • 6. The Solution
      • Create a way for themes to allow their The Loop content to be replaced without making the template files more difficult to create or maintain.
      • Create a way for plugins to register custom loop handlers that can replace the theme's default loop.
      • Create an HTML and class standard that provides a consistent foundation that themes can style
  • 7. Following in the Footsteps of Sidebars
      • WordPress already has a great example for a standardized way for themes and plugins to coexist and cooperate: Sidebars and Widgets.
      • The power of sidebars and widgets hinges off of the dynamic_sidebar function. This function allows themes to provide default content for sidebars that can be replaced by widgets.
      • The key is to wrap the default theme content in an if statement that uses the results of dynamic_sidebar as the conditional.
  • 8.
    • if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>
    •     <li id=&quot;search&quot; class=&quot;widget-container widget_search&quot;>
    •         <?php get_search_form(); ?>
    •     </li>
    •     <li id=&quot;archives&quot; class=&quot;widget-container&quot;>
    •         <h3 class=&quot;widget-title&quot;>Archives</h3>
    •         <ul>
    •             <?php wp_get_archives( 'type=monthly' ); ?>
    •         </ul>
    •     </li>
    •     <li id=&quot;meta&quot; class=&quot;widget-container&quot;>
    •         <h3 class=&quot;widget-title&quot;>Meta</h3>
    •         <ul>
    •             <?php wp_register(); ?>
    •             <li><?php wp_loginout(); ?></li>
    •             <?php wp_meta(); ?>
    •         </ul>
    •     </li>
    • <?php endif; // end primary widget area ?>
  • 9. Standard Loop: The Sidebar Way
    • <?php get_header(); ?>
    • <?php if ( ! dynamic_loop() ) : ?>
    •      <div class=&quot;loop&quot; >
    •         <?php if (have_posts()) : ?>
    •             <?php while (have_posts()) : the_post(); ?>
    •                 <div class=&quot;loop-content&quot; >
    •                     <div <?php post_class( 'hentry' ) ?> id=&quot;post-<?php the_ID(); ?>&quot;>
    •                         <h2 class=&quot;entry-title&quot; ><?php the_title() ?></h2>
    •                         <div class=&quot;entry-meta&quot; ><?php the_time('F jS, Y') ?></div>
    •                         <div class=&quot;entry-content&quot; >
    •                             <?php the_content(); ?>
    •                         </div>
    •                         <div class=&quot;entry-meta&quot; ><p><?php the_tags() ?></p></div>
    •                     </div>
    •                 </div>
    •             <?php endwhile; ?>
    •             <div class=&quot;loop-utility&quot; >
    •                 <div class=&quot;alignleft&quot;><?php next_posts_link() ?></div>
    •                 <div class=&quot;alignright&quot;><?php previous_posts_link() ?></div>
    •             </div>
    •         <?php endif; ?>
    •     </div>
    • <?php endif; ?>
    • <?php get_sidebar(); ?>
    • <?php get_footer(); ?>
  • 10. The Solution
      • Create a way for themes to allow their The Loop content to be replaced without making the template files more difficult to create or maintain.
      • Create a way for plugins to register custom loop handlers that can replace the theme's default loop.
      • Create an HTML and class standard that provides a consistent foundation that themes can style
  • 11. Registering Dynamic Loop Handlers
      • Themes use the register_sidebar() function to tell WordPress that a sidebar location is available. The arguments give the area a name and configure additional settings.
      • What we need is a way of registering callbacks that plugins can use, not named areas that can be displayed on the back-end. We need a different solution.
  • 12. Registering Dynamic Loop Handlers
    • The registration function should accept:
      • A function callback that decides whether or not the loop should be replaced. If the loop is to be replaced, the callback should run code that generates the new loop.
      • An array of arguments to be passed to the callback.
      • A priority in the same fashion as actions and filters.
  • 13. register_dynamic_loop_handler
    • function register_dynamic_loop_handler(
    •      $function,
    •      $args = array(),
    •      $priority = 10
    • ) {
    •     global $dynamic_loop_handlers;
    •     $dynamic_loop_handlers[$priority][] =
    •          array( $function, $args );
    • }
  • 14. Calling Dynamic Loop Handlers
    • Now all we need is a function that can be used in theme templates to run through the registered callbacks in order of priority.
    • Keeping with the dynamic_sidebar logic, the callback handler should return false if the default loop should be displayed and return true if the default loop is overridden and should not be displayed.
  • 15. dynamic_loop()
    • function dynamic_loop() {
    •     global $dynamic_loop_handlers;
    •     if ( empty( $dynamic_loop_handlers ) )
    •         return false;
    •     ksort( $dynamic_loop_handlers );
    •     foreach ( $dynamic_loop_handlers as $handlers ) {
    •         foreach ( $handlers as $callback ) {
    •             list( $function, $args ) = $callback;
    •             if ( is_callable( $function ) ) {
    •                 $result = call_user_func_array(
    •                      $function, $args
    •                  );
    •                 if ( false != $result )
    •                     return true;
    •         }
    •     }
    •     return false;
    • }
  • 16. Standardized HTML and Class Structure
    • While being able to replace The Loop is a big step forward, it will be of limited benefit if the new content uses a completely different structure than the theme. The result will likely be unstyled and will look out of place.
    • The final piece that ties everything together is a HTML and Class structure that establishes a foundation of styling that allows plugin content to look like it belongs on the site.
  • 17.
    • <div class=&quot;loop&quot;>
    •     <div class=&quot;loop-title&quot;></div>?    
    •     <div class=&quot;loop-utility loop-utility-above&quot;></div>*
    •     <div class=&quot;loop-meta loop-meta-above&quot;></div>*
    •     <div class=&quot;loop-content&quot;>
    •         <div class=&quot;loop-utility loop-utility-above&quot;></div>*
    •         <div class=&quot;loop-meta loop-meta-above&quot;></div>*
    •         <div class=&quot;hentry&quot;>+
    •             <div class=&quot;entry-title&quot;></div>?
    •             <div class=&quot;entry-utility entry-utility-above&quot;></div>*
    •             <div class=&quot;entry-meta entry-meta-above&quot;></div>*
    •             <div class=&quot;entry-summary&quot;></div>?
    •             <div class=&quot;entry-content&quot;></div>?
    •             <div class=&quot;entry-meta entry-meta-below&quot;></div>*
    •             <div class=&quot;entry-utility entry-utility-below&quot;></div>*
    •         </div>
    •         <div class=&quot;loop-utility loop-utility-above&quot;></div>*
    •         <div class=&quot;loop-meta loop-meta-above&quot;></div>*
    •         <div id=&quot;comments&quot;>?
    •             <div id=&quot;comments-title&quot;></div>?
    •             <div class=&quot;commentlist&quot;>?
    •                 <div class=&quot;comment&quot;></div>*
    •             </div>
    •             <div id=&quot;respond&quot;>
    •                 <div id=&quot;reply-title&quot;></div>?
    •                 <form id=&quot;commentform&quot;></form>
    •             </div>
    •         </div>
    •         <div class=&quot;loop-utility loop-utility-above&quot;></div>*
    •         <div class=&quot;loop-meta loop-meta-above&quot;></div>*
    •     </div>
    •     <div class=&quot;loop-meta loop-meta-below&quot;></div>*
    •     <div class=&quot;loop-utility loop-utility-below&quot;></div>*
    • </div>
  • 18. Checking for Standard Theme Support
    • Themes that implement the Standard Loop can use add_theme_support('standard-loop') to indicate that they follow the standard. This allows plugins to use current_theme_supports('standard-loop') to determine the support offered by the current theme and take action as needed if it is not available.
  • 19. Possible Future Improvements
      • Adding more classes that are focused on content. These would be in the vein of &quot;Obligatory WordPress Classes&quot; that can be found in most themes of the past few years.
      • Creating a more advanced callback handler that allows for registering criteria for when the callback should be called (thus removing the decision logic from the callback handler).
  • 20. Call to Action
    • Are you a theme developer that wants an easier way to allow plugins to provide content that styles nicely in your theme?
    • Are you a plugin developer that wants to have a better way to add generated content inside the theme and also wants that generated content to automatically take on theme styling?
    • Go to and get involved
  • 21. For More Information
    • Go to to get sample code and HTML/Class structures.