SlideShare une entreprise Scribd logo
1  sur  25
WRITING EXTENSIBLE
WORDPRESS PLUGINS
      Allen Snook
AGENDA

• Inspiration

• Concept

• Actions    and Filters

• Planning

• Pitfalls

• Hands      On
INSPIRATION


• Shane Pearlman’s
 presentation at WordCamp
 Seattle 2012 on Modern
 Tribe’s experiences



   http://www.slideshare.net/shanepearlman/the-capitalist-in-the-coop-the-art-science-of-the-premium-wordpress-business
CONCEPT
What the users and visitors experience


                        Premium Plugin
                          (Not Free)

                   BASE Plugin
              (FREE - WordPress.Org)

            WordPress Core
REQUIREMENTS

    Feature        Free Version   Premium Version
   Reporting           ✔                ✔
PayPal Standard        ✔                ✔
Gravity Payments                        ✔
  CSV Export                            ✔
MailChimp Integ.                        ✔
Link to Premium        ✔
GETTING HOOKED


• Actions

 • add_action($tag, $function   [, $priority [, $args]]);

 • do_action($tag, $arg);

 • remove_action($tag, $function    [, $priority [, $args]]);
function dgx_cart_queue_stylesheet() {
!    $styleurl = plugins_url('/css/styles.css', __FILE__);
!    wp_register_style('dgx_cart_css', $styleurl);
!    wp_enqueue_style('dgx_cart_css');
}
add_action('wp_print_styles', 'dgx_cart_queue_stylesheet');
GETTING HOOKED


• Filters

  • add_filter($tag, $function   [, $priority [, $args]]);

  • apply_filters($tag, $value   [,$var ...]);

  • remove_filter($tag, $function     [, $priority [, $args]]);
function dgx_social_butterfly_filter($content)
{
!    global $post;
!    if (is_single())
!    {
!    ! $content .= “<p>Appended to the content</p>”;
!    }
!    return $content;
}
add_filter('the_content', 'dgx_social_butterfly_filter');
PRIORITY AND ARGS

• Both add_action and
 add_filter accept two
 additional arguments -
 priority and the # of
 arguments

• You can use priority to
 arrange order of execution -
 think of it as a way of
 arranging cars on a train
PRIORITY


• For   example, instead of
        $content   =   dgx_donate_paypalstd_warning_section($content);
        $content   =   dgx_donate_get_donation_section($content);
        $content   =   dgx_donate_get_tribute_section($content);
        $content   =   dgx_donate_get_donor_section($content);
        $content   =   dgx_donate_get_billing_section($content);
        $content   =   dgx_donate_paypalstd_payment_section($content);
PRIORITY


• You   could do
        add_filter('dgx_donate_form',   'dgx_donate_paypalstd_warning_section', 1);
        add_filter('dgx_donate_form',   'dgx_donate_get_donation_section', 3);
        add_filter('dgx_donate_form',   'dgx_donate_get_tribute_section', 5);
        add_filter('dgx_donate_form',   'dgx_donate_get_donor_section', 7);
        add_filter('dgx_donate_form',   'dgx_donate_get_billing_section', 9);
        add_filter('dgx_donate_form',   'dgx_donate_paypalstd_payment_section', 11);


        $content = apply_filters('dgx_donate_form', $content);
WHEN TO USE WHICH


• In
   general, you will want to use filters for places where you
 need to return a value, and actions where you don’t

• For
    example, filters are great for customizing shortcodes since
 shortcodes don’t echo (they return)
MIND WARP


• Most of the time we have used add_action and add_filter as a
 single item, by itself, as we hooked into WordPress the
 platform

• When  writing extensible plugins, we need to mind warp a bit -
 our plugin becomes the platform - and you have to shift your
 perspective as a result
PLANNING

• Start
      with mockups of each
 view the user or visitor can
 see

• Identify
         in those mockups
 places where content needs
 to be added, removed or
 changed

• Use   your requirements
PLANNING 2

• Next, thinkabout the
 processing that needs to
 happen “behind the scenes”
 for specific events, e.g.
 adding a new post

• Think
      about what processing
 needs to “hook in” for the
 premium version
PLANNING 3


• For layers, like JavaScript,
 where you don’t have
 immediate access to
 WordPress actions and
 filters, use a data driven
 approach to “pass through”
 in a data agnostic fashion
JAVASCRIPT TRICKS


// Get the form data
var formData = {};
jQuery.each(jQuery('#dgx-cart-form').serializeArray(), function(i, field) {
         formData[field.name] = field.value;
         });

// Send the form data
formData['nonce'] = dgxCartAjax.nonce;
formData['action'] = 'dgx_cart_ajax_checkout1';
jQuery.post(dgxCartAjax.ajaxurl, formData, dgxCartCallback);
PLACES TO HOOK


• Thingsthat lend themselves to looping like rows in a table,
 sections in a form, columns on a settings page

• Save   actions (pass the postID as an argument)

• Footers   (great for adding/removing “upgrade to pro” link)
PITFALLS TO AVOID

• Your   base plugin could get deactivated

 • Ifyour extension plugin is calling functions in your base
   plugin, you should test for the base function before using it
   (e.g. if_function_exists)

• Don’t
      hook things that don’t need hooking (meta boxes,
 submenus)

• Don’t   expect to have all hooks identified in 1.0
KEY TAKE-AWAYS

• Think   of your base plugin as a platform

• Lookfor places where you will want to be able to insert / re-
 order content and processing

• Use   priority to arrange the “cars on the train”

• Keep    hook-unfriendly layers data agnostic

• Don’t   count on your base plugin always being there
HANDS ON
http://pastebin.com/m7RGJMHr

http://pastebin.com/HgXXhAUz
THANK YOU
allen@designgeneers.com
      @AllenSnook

Contenu connexe

Tendances

Image manipulation in WordPress 3.5
Image manipulation in WordPress 3.5Image manipulation in WordPress 3.5
Image manipulation in WordPress 3.5Marko Heijnen
 
PHPUnit with Mocking and Crawling
PHPUnit with Mocking and CrawlingPHPUnit with Mocking and Crawling
PHPUnit with Mocking and CrawlingTrung x
 
Render API - Pavel Makhrinsky
Render API - Pavel MakhrinskyRender API - Pavel Makhrinsky
Render API - Pavel MakhrinskyDrupalCampDN
 
Data::FormValidator Simplified
Data::FormValidator SimplifiedData::FormValidator Simplified
Data::FormValidator SimplifiedFred Moyer
 
WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3Mizanur Rahaman Mizan
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data ModelAttila Jenei
 
Geodaten & Drupal 7
Geodaten & Drupal 7Geodaten & Drupal 7
Geodaten & Drupal 7Michael Milz
 
Building secured wordpress themes and plugins
Building secured wordpress themes and pluginsBuilding secured wordpress themes and plugins
Building secured wordpress themes and pluginsTikaram Bhandari
 
Optimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page AppsOptimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page AppsMorgan Stone
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesEyal Vardi
 

Tendances (12)

Image manipulation in WordPress 3.5
Image manipulation in WordPress 3.5Image manipulation in WordPress 3.5
Image manipulation in WordPress 3.5
 
PHPUnit with Mocking and Crawling
PHPUnit with Mocking and CrawlingPHPUnit with Mocking and Crawling
PHPUnit with Mocking and Crawling
 
Render API - Pavel Makhrinsky
Render API - Pavel MakhrinskyRender API - Pavel Makhrinsky
Render API - Pavel Makhrinsky
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
Data::FormValidator Simplified
Data::FormValidator SimplifiedData::FormValidator Simplified
Data::FormValidator Simplified
 
WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3WordPress Theme Design and Development Workshop - Day 3
WordPress Theme Design and Development Workshop - Day 3
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data Model
 
Geodaten & Drupal 7
Geodaten & Drupal 7Geodaten & Drupal 7
Geodaten & Drupal 7
 
Building secured wordpress themes and plugins
Building secured wordpress themes and pluginsBuilding secured wordpress themes and plugins
Building secured wordpress themes and plugins
 
Optimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page AppsOptimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page Apps
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource Services
 

Similaire à Writing extensible Word press-plugins

Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Mike Schinkel
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoJoseph Dolson
 
WordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a FrameworkWordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a FrameworkExove
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
WordPress plugin #3
WordPress plugin #3WordPress plugin #3
WordPress plugin #3giwoolee
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction DjangoWade Austin
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPOscar Merida
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Yevhen Kotelnytskyi
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Ian Wilson
 
So S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeNeil Crookes
 
WordPress development paradigms, idiosyncrasies and other big words
WordPress development paradigms, idiosyncrasies and other big wordsWordPress development paradigms, idiosyncrasies and other big words
WordPress development paradigms, idiosyncrasies and other big wordsTomAuger
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Resource Routing in ExpressionEngine
Resource Routing in ExpressionEngineResource Routing in ExpressionEngine
Resource Routing in ExpressionEngineMichaelRog
 
Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerJoomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerSteven Pignataro
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...WordCamp Sydney
 
You're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaYou're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaChris Scott
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksEdmund Chan
 

Similaire à Writing extensible Word press-plugins (20)

Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp Chicago
 
WordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a FrameworkWordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a Framework
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
WordPress plugin #3
WordPress plugin #3WordPress plugin #3
WordPress plugin #3
 
Introduction Django
Introduction DjangoIntroduction Django
Introduction Django
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
 
So S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better Code
 
WordPress development paradigms, idiosyncrasies and other big words
WordPress development paradigms, idiosyncrasies and other big wordsWordPress development paradigms, idiosyncrasies and other big words
WordPress development paradigms, idiosyncrasies and other big words
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Resource Routing in ExpressionEngine
Resource Routing in ExpressionEngineResource Routing in ExpressionEngine
Resource Routing in ExpressionEngine
 
Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerJoomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
 
You're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp AtlantaYou're Doing it Wrong - WordCamp Atlanta
You're Doing it Wrong - WordCamp Atlanta
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - Hooks
 

Dernier

KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...Cara Menggugurkan Kandungan 087776558899
 
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDeepika Singh
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theorydrae5
 
the Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentationthe Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentationbrynpueblos04
 
February 2024 Recommendations for newsletter
February 2024 Recommendations for newsletterFebruary 2024 Recommendations for newsletter
February 2024 Recommendations for newsletterssuserdfec6a
 
WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxpadhand000
 
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...mitaliverma221
 
March 2023 Recommendations for newsletter
March 2023 Recommendations for newsletterMarch 2023 Recommendations for newsletter
March 2023 Recommendations for newsletterssuserdfec6a
 
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptx
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptxSIKP311 Sikolohiyang Pilipino - Ginhawa.pptx
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptxStephenMino
 
2023 - Between Philosophy and Practice: Introducing Yoga
2023 - Between Philosophy and Practice: Introducing Yoga2023 - Between Philosophy and Practice: Introducing Yoga
2023 - Between Philosophy and Practice: Introducing YogaRaphaël Semeteys
 

Dernier (10)

KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
KLINIK BATA Jual obat penggugur kandungan 087776558899 ABORSI JANIN KEHAMILAN...
 
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Dadar West Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theory
 
the Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentationthe Husband rolesBrown Aesthetic Cute Group Project Presentation
the Husband rolesBrown Aesthetic Cute Group Project Presentation
 
February 2024 Recommendations for newsletter
February 2024 Recommendations for newsletterFebruary 2024 Recommendations for newsletter
February 2024 Recommendations for newsletter
 
WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptx
 
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...
Call Girls In Mumbai Just Genuine Call ☎ 7738596112✅ Call Girl Andheri East G...
 
March 2023 Recommendations for newsletter
March 2023 Recommendations for newsletterMarch 2023 Recommendations for newsletter
March 2023 Recommendations for newsletter
 
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptx
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptxSIKP311 Sikolohiyang Pilipino - Ginhawa.pptx
SIKP311 Sikolohiyang Pilipino - Ginhawa.pptx
 
2023 - Between Philosophy and Practice: Introducing Yoga
2023 - Between Philosophy and Practice: Introducing Yoga2023 - Between Philosophy and Practice: Introducing Yoga
2023 - Between Philosophy and Practice: Introducing Yoga
 

Writing extensible Word press-plugins

  • 2. AGENDA • Inspiration • Concept • Actions and Filters • Planning • Pitfalls • Hands On
  • 3. INSPIRATION • Shane Pearlman’s presentation at WordCamp Seattle 2012 on Modern Tribe’s experiences http://www.slideshare.net/shanepearlman/the-capitalist-in-the-coop-the-art-science-of-the-premium-wordpress-business
  • 4.
  • 5.
  • 6. CONCEPT What the users and visitors experience Premium Plugin (Not Free) BASE Plugin (FREE - WordPress.Org) WordPress Core
  • 7. REQUIREMENTS Feature Free Version Premium Version Reporting ✔ ✔ PayPal Standard ✔ ✔ Gravity Payments ✔ CSV Export ✔ MailChimp Integ. ✔ Link to Premium ✔
  • 8. GETTING HOOKED • Actions • add_action($tag, $function [, $priority [, $args]]); • do_action($tag, $arg); • remove_action($tag, $function [, $priority [, $args]]);
  • 9. function dgx_cart_queue_stylesheet() { ! $styleurl = plugins_url('/css/styles.css', __FILE__); ! wp_register_style('dgx_cart_css', $styleurl); ! wp_enqueue_style('dgx_cart_css'); } add_action('wp_print_styles', 'dgx_cart_queue_stylesheet');
  • 10. GETTING HOOKED • Filters • add_filter($tag, $function [, $priority [, $args]]); • apply_filters($tag, $value [,$var ...]); • remove_filter($tag, $function [, $priority [, $args]]);
  • 11. function dgx_social_butterfly_filter($content) { ! global $post; ! if (is_single()) ! { ! ! $content .= “<p>Appended to the content</p>”; ! } ! return $content; } add_filter('the_content', 'dgx_social_butterfly_filter');
  • 12. PRIORITY AND ARGS • Both add_action and add_filter accept two additional arguments - priority and the # of arguments • You can use priority to arrange order of execution - think of it as a way of arranging cars on a train
  • 13. PRIORITY • For example, instead of $content = dgx_donate_paypalstd_warning_section($content); $content = dgx_donate_get_donation_section($content); $content = dgx_donate_get_tribute_section($content); $content = dgx_donate_get_donor_section($content); $content = dgx_donate_get_billing_section($content); $content = dgx_donate_paypalstd_payment_section($content);
  • 14. PRIORITY • You could do add_filter('dgx_donate_form', 'dgx_donate_paypalstd_warning_section', 1); add_filter('dgx_donate_form', 'dgx_donate_get_donation_section', 3); add_filter('dgx_donate_form', 'dgx_donate_get_tribute_section', 5); add_filter('dgx_donate_form', 'dgx_donate_get_donor_section', 7); add_filter('dgx_donate_form', 'dgx_donate_get_billing_section', 9); add_filter('dgx_donate_form', 'dgx_donate_paypalstd_payment_section', 11); $content = apply_filters('dgx_donate_form', $content);
  • 15. WHEN TO USE WHICH • In general, you will want to use filters for places where you need to return a value, and actions where you don’t • For example, filters are great for customizing shortcodes since shortcodes don’t echo (they return)
  • 16. MIND WARP • Most of the time we have used add_action and add_filter as a single item, by itself, as we hooked into WordPress the platform • When writing extensible plugins, we need to mind warp a bit - our plugin becomes the platform - and you have to shift your perspective as a result
  • 17. PLANNING • Start with mockups of each view the user or visitor can see • Identify in those mockups places where content needs to be added, removed or changed • Use your requirements
  • 18. PLANNING 2 • Next, thinkabout the processing that needs to happen “behind the scenes” for specific events, e.g. adding a new post • Think about what processing needs to “hook in” for the premium version
  • 19. PLANNING 3 • For layers, like JavaScript, where you don’t have immediate access to WordPress actions and filters, use a data driven approach to “pass through” in a data agnostic fashion
  • 20. JAVASCRIPT TRICKS // Get the form data var formData = {}; jQuery.each(jQuery('#dgx-cart-form').serializeArray(), function(i, field) { formData[field.name] = field.value; }); // Send the form data formData['nonce'] = dgxCartAjax.nonce; formData['action'] = 'dgx_cart_ajax_checkout1'; jQuery.post(dgxCartAjax.ajaxurl, formData, dgxCartCallback);
  • 21. PLACES TO HOOK • Thingsthat lend themselves to looping like rows in a table, sections in a form, columns on a settings page • Save actions (pass the postID as an argument) • Footers (great for adding/removing “upgrade to pro” link)
  • 22. PITFALLS TO AVOID • Your base plugin could get deactivated • Ifyour extension plugin is calling functions in your base plugin, you should test for the base function before using it (e.g. if_function_exists) • Don’t hook things that don’t need hooking (meta boxes, submenus) • Don’t expect to have all hooks identified in 1.0
  • 23. KEY TAKE-AWAYS • Think of your base plugin as a platform • Lookfor places where you will want to be able to insert / re- order content and processing • Use priority to arrange the “cars on the train” • Keep hook-unfriendly layers data agnostic • Don’t count on your base plugin always being there

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n