Creating ExtensiblePlugins for WordPressHristo ChakarovWordUp! Conference, Sofia, 2013
Who am I?● Im in the web since 2002○ professionally since 2007● Currently working as a Senior JavaScriptdeveloper @ Netcli...
Why WordPress?● Which is the best CMS in the World?○ In fact, theres no such● However, WordPress is much, much betterthan ...
Today:1. Why do we need to make plugins extensible2. Anatomy of WordPress hooks3. Differences between actions & filters+ e...
Quick Questions● How many of you have contributed at least 1plugin in WordPress Plugin Directory?● How often do you feel t...
Consider the following scenario:1. You need extra functionality for your WPproject2. You enjoy a plugin and you download i...
...Damn! My changes got lost!Shooting at the developer wont solve the problem
What can be done?Nothing.But at least we can start making our ownplugins extensible and make the World better :)
Benefits of extensible plugin● other developers can easily extend yourplugins so they serve their needs● code reuse - we m...
OK, but how do we make our WPplugin extensible?How do you extend WordPress?● by using the platforms hooksadd_actionremove_...
How actions & filters work(Minimum Theory)● add_action & add_filter register handlers(functions) to be executed on certain...
Difference between Actions & Filters● filters accept, modify & return data○ strings○ arrays○ objects● actions just execute...
● to modify strings (can be HTML or any other)// in your core pluginecho apply_filters(my_plugin_html,<strong>Hello, world...
Use filters:● for HTML its better to work on DOM level○ Simple HTML DOM library is good for that$dom = str_get_html(<b>Hel...
Use filters:● to modify WP Query// define initial set of query params$query_args = array(post_type => books,author => 3);$...
Use actions:● to spice HTML markup
Use actions:● to print resources// in the core plugindo_action( myplugin_print_resources );// in the extending pluginadd_a...
Lets make our plugin more OOP!● define a main class for your plugin● instantiate the class on WordPress <init>class MyPlug...
Overwrite● define extending class & overwrite base● instantiate the class on WordPress <init>class ExtendingPlugin extends...
Tips● plan your actions & filters carefully○ think where you will need to provide a hook○ try to use descriptive & easy to...
● Image Widgethttp://wordpress.org/extend/plugins/image-widget/● NextGEN Galleryhttp://wordpress.org/extend/plugins/nextge...
Extending NextGEN Gallery pluginwith custom templateDemo
I recommend you to read● Anatomy of a WordPress Pluginhttp://www.packtpub.com/article/anatomy-wordpress-plugin● Inside Wor...
Time for questionsYour turn - word up!
mail (at) ickata.netblog.ickata.netfacebook.com/ickatanetgithub.com/ickataThank You!
Creating Extensible Plugins for WordPress
Upcoming SlideShare
Loading in...5
×

Creating Extensible Plugins for WordPress

599

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
599
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Creating Extensible Plugins for WordPress

  1. 1. Creating ExtensiblePlugins for WordPressHristo ChakarovWordUp! Conference, Sofia, 2013
  2. 2. Who am I?● Im in the web since 2002○ professionally since 2007● Currently working as a Senior JavaScriptdeveloper @ Netclime Inc.○ one product - SiteKreator (website builder)http://sitekreator.com● Love WordPress○ 2 plugins on the official plugins page○ several commercial projects on top of the platform○ co-founder of WordPress Bulgarian User Group(WPBGUG)http://wpbgug.org/
  3. 3. Why WordPress?● Which is the best CMS in the World?○ In fact, theres no such● However, WordPress is much, much betterthan its most famous competitors○ Great Admin UI○ Rich & well documented API○ Easy to extend■ you dont have to be very experiencedprogrammer in order to create a plugin○ Huge community■ tons of free & paid Themes & Plugins○ But last 2 might be problematic
  4. 4. Today:1. Why do we need to make plugins extensible2. Anatomy of WordPress hooks3. Differences between actions & filters+ examples4. Overwriting OOP-style plugins5. Tips6. Demo, plugin examples, good to read
  5. 5. Quick Questions● How many of you have contributed at least 1plugin in WordPress Plugin Directory?● How often do you feel the need to tweak a3rd party plugin in order to fit 100% in yourproject(s)?● When you start coding new plugin, have youever felt the need to reuse functionality fromanother your plugin?
  6. 6. Consider the following scenario:1. You need extra functionality for your WPproject2. You enjoy a plugin and you download it3. Theres that very tiny thing that this plugindoes not bring to you4. You modify plugins source code5. You forget about modifying the source6. A new version of the plugin is available. Youupdate your copy and after the update youlook at your site and...
  7. 7. ...Damn! My changes got lost!Shooting at the developer wont solve the problem
  8. 8. What can be done?Nothing.But at least we can start making our ownplugins extensible and make the World better :)
  9. 9. Benefits of extensible plugin● other developers can easily extend yourplugins so they serve their needs● code reuse - we may have a base plugin(core) and a lot of extensions (other plugins)built on top of it
  10. 10. OK, but how do we make our WPplugin extensible?How do you extend WordPress?● by using the platforms hooksadd_actionremove_actionadd_filterremove_filterIts the same if you want to make your pluginextensible - just register your own hooks!● do_action● apply_filters
  11. 11. How actions & filters work(Minimum Theory)● add_action & add_filter register handlers(functions) to be executed on certain event( hook_id, fn_name, priority, num_args )● do_action & apply_filters execute allhandlers when called
  12. 12. Difference between Actions & Filters● filters accept, modify & return data○ strings○ arrays○ objects● actions just execute○ actions are just like events - they "trigger"
  13. 13. ● to modify strings (can be HTML or any other)// in your core pluginecho apply_filters(my_plugin_html,<strong>Hello, world!</strong>);// in extending pluginadd_filter( my_plugin_html,custom_html_filter );function custom_html_filter( $html ) {return <div> . $html . </div>;}Use filters:
  14. 14. Use filters:● for HTML its better to work on DOM level○ Simple HTML DOM library is good for that$dom = str_get_html(<b>Hello, world!</b>);apply_filters( my_plugin_dom, $dom );echo $dom->save();add_filter( my_plugin_dom,custom_dom_filter );function custom_dom_filter( $dom ) {// replace the <b> with <strong>foreach ( $b as $dom->find(b) )$b->outerhtml = <strong>.$b->innertext.</strong>;return $dom;
  15. 15. Use filters:● to modify WP Query// define initial set of query params$query_args = array(post_type => books,author => 3);$books = new WP_Query( apply_filters(myplugin_query, $query_args) );add_filter(myplugin_query, modify_query);function modify_query( $query_args ) {$query_args[posts_per_page] = 5;return $query_args;
  16. 16. Use actions:● to spice HTML markup
  17. 17. Use actions:● to print resources// in the core plugindo_action( myplugin_print_resources );// in the extending pluginadd_action(myplugin_print_resources,myplugin_print_scripts);function myplugin_print_scripts() {echo <script src=".."></script>}
  18. 18. Lets make our plugin more OOP!● define a main class for your plugin● instantiate the class on WordPress <init>class MyPlugin {function myMethod() {// ...}}add_action( init, myplugin_init );function myplugin_init() {global $plugin_instance;$plugin_instance = new MyPlugin();}
  19. 19. Overwrite● define extending class & overwrite base● instantiate the class on WordPress <init>class ExtendingPlugin extends MyPlugin {function myMethod() {$this->parent(); // not necessary// .. extra functionality here}}remove_action( init, myplugin_init );add_action( init, extending_init );function extending_init() {global $plugin_instance;$plugin_instance = new ExtendingPlugin();}
  20. 20. Tips● plan your actions & filters carefully○ think where you will need to provide a hook○ try to use descriptive & easy to remember namesfilter_html (too generic)wpplg_tpl_html (what?)a_really_awesome_filter_html (too long)myplugin_filter_html (almost perfect)○ dont overhook● its good to create a documentation page● comment, comment, comment!○ comments can be your best friend
  21. 21. ● Image Widgethttp://wordpress.org/extend/plugins/image-widget/● NextGEN Galleryhttp://wordpress.org/extend/plugins/nextgen-gallery/● bbPresshttp://wordpress.org/extend/plugins/bbpress/● WPMLhttp://wpml.org/● WooCommercehttp://www.woothemes.com/woocommerce/Extensible plugins for inspiration
  22. 22. Extending NextGEN Gallery pluginwith custom templateDemo
  23. 23. I recommend you to read● Anatomy of a WordPress Pluginhttp://www.packtpub.com/article/anatomy-wordpress-plugin● Inside WordPress Actions And Filtershttp://wp.smashingmagazine.com/2012/02/16/inside-wordpress-actions-filters/● Writing Extensible Plugins With Actions andFiltershttp://wp.tutsplus.com/tutorials/plugins/writing-extensible-plugins-with-actions-and-filters/● 5 Things I’ve Learned About Writing FlexiblePluginshttp://www.kungfugrep.com/5-learned-writing-flexible-plugins/
  24. 24. Time for questionsYour turn - word up!
  25. 25. mail (at) ickata.netblog.ickata.netfacebook.com/ickatanetgithub.com/ickataThank You!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×