Writing extensible Word press-plugins
Upcoming SlideShare
Loading in...5
×
 

Writing extensible Word press-plugins

on

  • 621 views

 

Statistics

Views

Total Views
621
Views on SlideShare
618
Embed Views
3

Actions

Likes
1
Downloads
10
Comments
0

1 Embed 3

https://twitter.com 3

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 Writing extensible Word press-plugins Presentation Transcript

  • WRITING EXTENSIBLEWORDPRESS 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
  • CONCEPTWhat 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 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);
  • 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 ONhttp://pastebin.com/m7RGJMHrhttp://pastebin.com/HgXXhAUz
  • THANK YOUallen@designgeneers.com @AllenSnook