Hooking with WordPress

1,016 views
939 views

Published on

These are the presentation slides from the WordPress GTA Meetup presentation Hooking with WordPress: WordPress Plugins 103 as used on November 19, 2012

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

No Downloads
Views
Total views
1,016
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Hooking with WordPress

  1. 1. Hooking with WordPress WordPress Plugins 103
  2. 2. WordPress HooksWordPress Hooks• Actions• Filters
  3. 3. WordPress ActionsACTIONS ARE THE HOOKS THAT THEWORDPRESS CORE LAUNCHES AT SPECIFICPOINTS DURING EXECUTION, OR WHENSPECIFIC EVENTS OCCUR. YOUR PLUGIN CANSPECIFY THAT ONE OR MORE OF ITS PHPFUNCTIONS ARE EXECUTED AT THESE POINTS, ACTION API.USING THE~~ WORDPRESS CODEX
  4. 4. WordPress ActionsWordPress HooksActions• http://codex.wordpress.org/Plugin_API/Action_Reference
  5. 5. WordPress ActionsBasic Functions• do_action() – http://codex.wordpress.org/Function_Reference/do_action• add_action() – http://codex.wordpress.org/Function_Reference/add_action• remove_action() – http://codex.wordpress.org/Function_Reference/remove_action
  6. 6. WordPress ActionsBasic Functions• do_action() – http://codex.wordpress.org/Function_Reference/do_actionThis is where the action fires in the code,and where your hook will attach itself.Example: do_action( „my_action‟ );
  7. 7. WordPress ActionsBasic Functions• add_action() – http://codex.wordpress.org/Function_Reference/add_actionThis is what you use to add your action (orfunctionality) to the do_action() sequence.Example:add_action( „my_action‟, „my_functionality‟, 10 );
  8. 8. WordPress ActionsBasic Functions• remove_action() – http://codex.wordpress.org/Function_Reference/remove_actionThis is what you use to remove a specificaction that has been added … and itrequires a call to an action, too?!
  9. 9. WordPress ActionsBasic Functions• remove_action()Example:function remove_my_action(){ remove_action( „my_action‟, „my_functionality‟, 10 );}add_action( „init‟, „remove_my_action‟ );Note: the priority being used; and, adding tothe init action.
  10. 10. WordPress FiltersFILTERS ARE THE HOOKS THAT WORDPRESSLAUNCHES TO MODIFY TEXT OF VARIOUS TYPESBEFORE ADDING IT TO THE DATABASE ORSENDING IT TO THE BROWSER SCREEN. YOURPLUGIN CAN SPECIFY THAT ONE OR MORE OFITSPHP FUNCTIONS IS EXECUTED TO MODIFYSPECIFIC TYPES OF TEXT AT THESE TIMES,USING THE FILTER API.~~ WORDPRESS CODEX
  11. 11. WordPress FiltersWordPress HooksFilters• http://codex.wordpress.org/Plugin_API/Filter_Reference
  12. 12. WordPress FiltersBasic Functions• apply_filters – http://codex.wordpress.org/Function_Reference/apply_filters• add_filter – http://codex.wordpress.org/Function_Reference/add_filter• remove_filter – http://codex.wordpress.org/Function_Reference/remove_filter
  13. 13. WordPress FiltersBasic Functions• apply_filters – http://codex.wordpress.org/Function_Reference/apply_filtersThis is where the filter fires in the code, andwhere your hook will attach itself.Example:apply_filters( „my_filter‟, „my_original_value‟ );
  14. 14. WordPress FiltersBasic Functions• add_filter – http://codex.wordpress.org/Function_Reference/add_filterThis is what you use to add your filter (ornew value) to the apply_filters() item.Example:add_filter( „my_filter‟, „my_new_value‟ );
  15. 15. WordPress FiltersBasic Functions• remove_filter – http://codex.wordpress.org/Function_Reference/remove_filterThis is what you use to remove a specificfilter that has been used … and it is best tofollow the remove_action() example as thesafest method to implement.
  16. 16. WordPress FiltersBasic functions• remove_filter()Example:function remove_my_filter(){ remove_filter( „my_filter‟, „my_new_value‟, 20 );}add_action( „init‟, „remove_my_filter‟ );Note: the priority being used; and, adding tothe init action.
  17. 17. Hooking with WordPress1. Open your local test environment2. Make sure the BNS Bio plugin is installed and activated3. Add the [bns_bio] shortcode to a post4. Open your text editor5. Navigate to the BNS Bio folder6. Ready?
  18. 18. BNS Bio PluginThe BNS Bio plugin adds a simpleshortcode to your WordPress installationthat outputs the author details of the post itis added to.The plugin uses a different approach tooptions by utilizing “extension” pluginsversus an options panel. This is primarily forthe purposes of using BNS Bio as examplecode.
  19. 19. BNS Bio Structures• Uses the Class PHP structure – Uses a simple constuctor method • __construct() – Enqueues its own stylesheet with an optional call to a custom stylesheet • Scripts_and_Styles() – Collect the author details which it passes back to the shortcode `bns_bio` • author_block()
  20. 20. BNS Bio __construct()The constructor method of the BNS Bioclass calls an action to add theScripts_and_Styles functionality …/** Add Scripts and Styles */add_action( wp_enqueue_scripts, array( $this, Scripts_and_Styles ) );… note the difference from the earlierexample.
  21. 21. WordPress ActionsBasic Functions• add_action() – http://codex.wordpress.org/Function_Reference/add_actionExample:add_action( „my_action‟, „my_functionality‟, 10 );Using in a Class example:add_action( „my_action‟, array( $this, „my_functionality‟ ) );
  22. 22. BNS Bio author_block()• This is the main code of the plugin.• There are many instances of hooks (actions and filters) available to work with.Examples:do_action( bns_bio_before_all );apply_filters( bns_bio_author_url_text, …apply_filters( bns_bio_author_desc, …
  23. 23. BNS Bio BoxThis is an “extension plugin” for BNS Bio.It adds a rounded corner border, or box,around the BNS Bio output by hooking anadditional CSS container into the„bns_bio_before_all‟ and „bns_bio_after_all‟actions; then adding the appropriate styleproperties to create the look.
  24. 24. BNS Bio Box CodeThe essentials:• Define the functions to be used• Hook them into the appropriate action – add_action( bns_bio_before_all, bns_bio_open_box ); – add_action( bns_bio_after_all, bns_bio_close_box );• Add the stylesheet (using the common enqueue method; and another add_action call)
  25. 25. BNS Bio ListThis is another “extension plugin” for BNSBio that changes the output to an unorderedlist by hooking into the available actions andadding the appropriate HTML („ul‟ and „li‟)elements.It also includes its own enqueued stylesheetand calls an optional custom stylesheet ifavailable.
  26. 26. BNS Bio List CodeThe essentials:• Add the „ul‟ container: – add_action( bns_bio_before_all, function(){ echo <ul class="bns-bio-list">; }, 20 ); – add_action( bns_bio_after_all, function(){ echo </ul><!-- .bns-bio-list -->; }, 9 );• Add the „li‟ containers: – add_action( bns_bio_before_author_name, bns_bio_list_item ); – add_action( bns_bio_before_author_url, bns_bio_list_item ); – add_action( bns_bio_before_author_email, bns_bio_list_item ); – add_action( bns_bio_before_author_desc, bns_bio_list_item );NB: I used two different methods for adding thefunctionality: a call to a predefined function(„bns_bio_list_item‟) and anonymous functions1
  27. 27. BNS Bio List Notes1. The anonymous functions were used forexample purposes and may not work withversions of PHP earlier than 5.3.0 wherethey were introduced.
  28. 28. BNS Bio HideThis is another “extension plugin” for BNSBio that changes the output by hiding theauthor email details.There are actually three items that need tobe addressed to hide the email line in theoutput:• What displays before the email• What displays as the author (pre) email text• What displays as the author email
  29. 29. WordPress Plugins 103The essentials:• remove_action( bns_bio_before_author_email, bns_bio_list_item ); – (as applied to the init hook – see original „remove_action‟ example)• add_filter( bns_bio_author_email_text, __return_null, 100 );• add_filter( bns_bio_author_email, __return_null, 100 );Notes:• The remove_action priority is left as the default as the „bns_bio_list_item‟ function was added at the default priority (these priorities must match).• Filters are essentially a “last one standing” mechanism; and in this case, the WordPress „__return_null‟ function was called at a very late priority to help insure it is last to run and therefore the final output.
  30. 30. WordPress Plugins 103 Hooking with WordPress Questions and Answers
  31. 31. Credits Presented by Edward Caissiefor the WordPress GTA Meetup group on November 19, 2012. Contact Details: Email: edward.caissie@gmail.com URL: http://edwardcaissie.com
  32. 32. Quick ResourcesCodex References - Actions:http://codex.wordpress.org/Plugin_API/Action_Referencehttp://codex.wordpress.org/Function_Reference/do_actionhttp://codex.wordpress.org/Function_Reference/add_actionhttp://codex.wordpress.org/Function_Reference/remove_filterCodex References – Filters:http://codex.wordpress.org/Plugin_API/Filter_Referencehttp://codex.wordpress.org/Function_Reference/apply_filtershttp://codex.wordpress.org/Function_Reference/add_filterhttp://codex.wordpress.org/Function_Reference/remove_filter
  33. 33. WordPress Power Point Template Creditsm62 visualcommunications:http://www.m62.net/powerpoint-templates/technology-templates/wordpress-powerpoint-template/
  34. 34. WordPress Plugins 103: Hooking with WordPress Thank You.

×