際際滷

際際滷Share a Scribd company logo
Standardized Loop API The Next Big Thing?
Who Am I? Chris Jean chrisjean.com @chrisjean Developer for iThemes Core contributor Person who wants to make WordPress better for everyone
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.
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
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.
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
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.
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 ?>
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(); ?>
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
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.
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.
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 ); }
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.
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; }
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.
<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>
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.
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).
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 loopstandard.com and get involved
For More Information Go to standardloop.com to get sample code and HTML/Class structures.

More Related Content

What's hot (20)

How I Learned to Stop Worrying and Backup WordPress
How I Learned to Stop Worrying and Backup WordPressHow I Learned to Stop Worrying and Backup WordPress
How I Learned to Stop Worrying and Backup WordPress
Chris Jean
Rey Bango - HTML5: polyfills and shims
Rey Bango -  HTML5: polyfills and shimsRey Bango -  HTML5: polyfills and shims
Rey Bango - HTML5: polyfills and shims
StarTech Conference
Hardening WordPress - Friends of Search 2014 (WordPress Security)
Hardening WordPress - Friends of Search 2014 (WordPress Security)Hardening WordPress - Friends of Search 2014 (WordPress Security)
Hardening WordPress - Friends of Search 2014 (WordPress Security)
Bastian Grimm
Lecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITPLecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITP
yucefmerhi
Amazing WordPress & Productivity Tips
Amazing WordPress & Productivity TipsAmazing WordPress & Productivity Tips
Amazing WordPress & Productivity Tips
Tony Cecala, Ph.D.
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Bastian Grimm
Technical SEO for WordPress - 2019 edition
Technical SEO for WordPress - 2019 editionTechnical SEO for WordPress - 2019 edition
Technical SEO for WordPress - 2019 edition
Otto Kek辰l辰inen
Page Speed
Page SpeedPage Speed
Page Speed
Jon Henshaw
Fast by Default
Fast by DefaultFast by Default
Fast by Default
Abhay Kumar
Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017
Christian Heilmann
Jabber Bot
Jabber BotJabber Bot
Jabber Bot
Aizat Faiz
Responsible [digital] Home Ownership
Responsible [digital] Home OwnershipResponsible [digital] Home Ownership
Responsible [digital] Home Ownership
Denise (Dee) Teal
Facebook Social Plugins
Facebook Social PluginsFacebook Social Plugins
Facebook Social Plugins
Aizat Faiz
Word press guide_-_makeuseof.com
Word press guide_-_makeuseof.comWord press guide_-_makeuseof.com
Word press guide_-_makeuseof.com
Tan Pham
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nlJoomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Philip Locke
Progressive Enhancement
Progressive EnhancementProgressive Enhancement
Progressive Enhancement
Zach Leatherman
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
Bhushan Jawle
How to add watermark to image using php
How to add watermark to image using phpHow to add watermark to image using php
How to add watermark to image using php
YourBlogCoach1
Wrangling Large Scale Frontend Web Applications
Wrangling Large Scale Frontend Web ApplicationsWrangling Large Scale Frontend Web Applications
Wrangling Large Scale Frontend Web Applications
Ryan Roemer
A Beginner's Guide to WordPress - WordCamp Montreal 2012
A Beginner's Guide to WordPress - WordCamp Montreal 2012A Beginner's Guide to WordPress - WordCamp Montreal 2012
A Beginner's Guide to WordPress - WordCamp Montreal 2012
Kathryn Presner
How I Learned to Stop Worrying and Backup WordPress
How I Learned to Stop Worrying and Backup WordPressHow I Learned to Stop Worrying and Backup WordPress
How I Learned to Stop Worrying and Backup WordPress
Chris Jean
Rey Bango - HTML5: polyfills and shims
Rey Bango -  HTML5: polyfills and shimsRey Bango -  HTML5: polyfills and shims
Rey Bango - HTML5: polyfills and shims
StarTech Conference
Hardening WordPress - Friends of Search 2014 (WordPress Security)
Hardening WordPress - Friends of Search 2014 (WordPress Security)Hardening WordPress - Friends of Search 2014 (WordPress Security)
Hardening WordPress - Friends of Search 2014 (WordPress Security)
Bastian Grimm
Lecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITPLecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITP
yucefmerhi
Amazing WordPress & Productivity Tips
Amazing WordPress & Productivity TipsAmazing WordPress & Productivity Tips
Amazing WordPress & Productivity Tips
Tony Cecala, Ph.D.
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Hardening WordPress - SAScon Manchester 2013 (WordPress Security)
Bastian Grimm
Technical SEO for WordPress - 2019 edition
Technical SEO for WordPress - 2019 editionTechnical SEO for WordPress - 2019 edition
Technical SEO for WordPress - 2019 edition
Otto Kek辰l辰inen
Fast by Default
Fast by DefaultFast by Default
Fast by Default
Abhay Kumar
Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017Leveling up your JavaScipt - DrupalJam 2017
Leveling up your JavaScipt - DrupalJam 2017
Christian Heilmann
Responsible [digital] Home Ownership
Responsible [digital] Home OwnershipResponsible [digital] Home Ownership
Responsible [digital] Home Ownership
Denise (Dee) Teal
Facebook Social Plugins
Facebook Social PluginsFacebook Social Plugins
Facebook Social Plugins
Aizat Faiz
Word press guide_-_makeuseof.com
Word press guide_-_makeuseof.comWord press guide_-_makeuseof.com
Word press guide_-_makeuseof.com
Tan Pham
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nlJoomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Joomla wireframing Template - Joomladay Netherlands 2014 #jd14nl
Philip Locke
Progressive Enhancement
Progressive EnhancementProgressive Enhancement
Progressive Enhancement
Zach Leatherman
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
WordPress Security : What We Learnt When We Were Hacked : WordCamp Mumbai 2017
Bhushan Jawle
How to add watermark to image using php
How to add watermark to image using phpHow to add watermark to image using php
How to add watermark to image using php
YourBlogCoach1
Wrangling Large Scale Frontend Web Applications
Wrangling Large Scale Frontend Web ApplicationsWrangling Large Scale Frontend Web Applications
Wrangling Large Scale Frontend Web Applications
Ryan Roemer
A Beginner's Guide to WordPress - WordCamp Montreal 2012
A Beginner's Guide to WordPress - WordCamp Montreal 2012A Beginner's Guide to WordPress - WordCamp Montreal 2012
A Beginner's Guide to WordPress - WordCamp Montreal 2012
Kathryn Presner

Similar to WordPress Standardized Loop API (20)

Drupal 7 Theming - what's new
Drupal 7 Theming - what's newDrupal 7 Theming - what's new
Drupal 7 Theming - what's new
Marek Sotak
The Basics Of Page Creation
The Basics Of Page CreationThe Basics Of Page Creation
The Basics Of Page Creation
Wildan Maulana
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
Magecom Ukraine
WordPress Development Confoo 2010
WordPress Development Confoo 2010WordPress Development Confoo 2010
WordPress Development Confoo 2010
Brendan Sera-Shriar
Optimizing Drupal for Mobile Devices
Optimizing Drupal for Mobile DevicesOptimizing Drupal for Mobile Devices
Optimizing Drupal for Mobile Devices
Sugree Phatanapherom
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
Wen-Tien Chang
Html5
Html5Html5
Html5
dotNETUserGroupDnipro
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
Suite Solutions
Open Power Template 2 presentation
Open Power Template 2 presentationOpen Power Template 2 presentation
Open Power Template 2 presentation
Tomasz Jdrzejewski
Various Ways of Using WordPress
Various Ways of Using WordPressVarious Ways of Using WordPress
Various Ways of Using WordPress
Nick La
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
brynary
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplication
olegmmiller
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
Struts2
Struts2Struts2
Struts2
yuvalb
How to learn to build your own PHP framework
How to learn to build your own PHP frameworkHow to learn to build your own PHP framework
How to learn to build your own PHP framework
Dinh Pham
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.frameworkHanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
Front End on Rails
Front End on RailsFront End on Rails
Front End on Rails
Justin Halsall
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
webhostingguy
惺惘惷 忰 惘惆惡惘愕
惺惘惷 忰 惘惆惡惘愕惺惘惷 忰 惘惆惡惘愕
惺惘惷 忰 惘惆惡惘愕
Mohammed SAHLI
Drupal 7 Theming - what's new
Drupal 7 Theming - what's newDrupal 7 Theming - what's new
Drupal 7 Theming - what's new
Marek Sotak
The Basics Of Page Creation
The Basics Of Page CreationThe Basics Of Page Creation
The Basics Of Page Creation
Wildan Maulana
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
弌亳亠仄舒 亠仆亟亠亳仆亞舒 于 Magento
Magecom Ukraine
WordPress Development Confoo 2010
WordPress Development Confoo 2010WordPress Development Confoo 2010
WordPress Development Confoo 2010
Brendan Sera-Shriar
Optimizing Drupal for Mobile Devices
Optimizing Drupal for Mobile DevicesOptimizing Drupal for Mobile Devices
Optimizing Drupal for Mobile Devices
Sugree Phatanapherom
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
Wen-Tien Chang
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
Suite Solutions
Open Power Template 2 presentation
Open Power Template 2 presentationOpen Power Template 2 presentation
Open Power Template 2 presentation
Tomasz Jdrzejewski
Various Ways of Using WordPress
Various Ways of Using WordPressVarious Ways of Using WordPress
Various Ways of Using WordPress
Nick La
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
brynary
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplication
olegmmiller
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
Struts2
Struts2Struts2
Struts2
yuvalb
How to learn to build your own PHP framework
How to learn to build your own PHP frameworkHow to learn to build your own PHP framework
How to learn to build your own PHP framework
Dinh Pham
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.frameworkHanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
webhostingguy
惺惘惷 忰 惘惆惡惘愕
惺惘惷 忰 惘惆惡惘愕惺惘惷 忰 惘惆惡惘愕
惺惘惷 忰 惘惆惡惘愕
Mohammed SAHLI

WordPress Standardized Loop API

  • 1. Standardized Loop API The Next Big Thing?
  • 2. Who Am I? Chris Jean chrisjean.com @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 loopstandard.com and get involved
  • 21. For More Information Go to standardloop.com to get sample code and HTML/Class structures.