Writing extensible Word press-plugins

687 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
687
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Writing extensible Word press-plugins

    1. 1. WRITING EXTENSIBLEWORDPRESS PLUGINS Allen Snook
    2. 2. AGENDA• Inspiration• Concept• Actions and Filters• Planning• Pitfalls• Hands On
    3. 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. 4. CONCEPTWhat the users and visitors experience Premium Plugin (Not Free) BASE Plugin (FREE - WordPress.Org) WordPress Core
    5. 5. REQUIREMENTS Feature Free Version Premium Version Reporting ✔ ✔PayPal Standard ✔ ✔Gravity Payments ✔ CSV Export ✔MailChimp Integ. ✔Link to Premium ✔
    6. 6. GETTING HOOKED• Actions • add_action($tag, $function [, $priority [, $args]]); • do_action($tag, $arg); • remove_action($tag, $function [, $priority [, $args]]);
    7. 7. 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);
    8. 8. GETTING HOOKED• Filters • add_filter($tag, $function [, $priority [, $args]]); • apply_filters($tag, $value [,$var ...]); • remove_filter($tag, $function [, $priority [, $args]]);
    9. 9. 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);
    10. 10. 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
    11. 11. 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);
    12. 12. 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);
    13. 13. 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)
    14. 14. 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
    15. 15. 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
    16. 16. 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
    17. 17. 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
    18. 18. JAVASCRIPT TRICKS// Get the form datavar formData = {};jQuery.each(jQuery(#dgx-cart-form).serializeArray(), function(i, field) { formData[field.name] = field.value; });// Send the form dataformData[nonce] = dgxCartAjax.nonce;formData[action] = dgx_cart_ajax_checkout1;jQuery.post(dgxCartAjax.ajaxurl, formData, dgxCartCallback);
    19. 19. 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)
    20. 20. 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
    21. 21. 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
    22. 22. HANDS ONhttp://pastebin.com/m7RGJMHrhttp://pastebin.com/HgXXhAUz
    23. 23. THANK YOUallen@designgeneers.com @AllenSnook

    ×