Stop Hacking Wordpress   Start Working With It!         Charly Leetham    www.AskCharlyLeetham.com
Share This...    @charlyjl    charlyleetham
What will we cover•   What is the Wordpress API•   What are Filters, Actions & Hooks•   Creating & Calling Actions•   Acti...
The API...• Wordpress API is designed for anyone to  extend the base WP Functionality• Create Plugins or Themes• Integrate...
Filters...• Modify content.... That’s it!• For example:  – Change post / page content  – Change widget titles  – RSS Feeds...
Actions• Do something ... when called...• Allow the ‘execution’ of functions at fixed  points in the code• For example  – ...
• Filters & Actions are basically the same  thing...• Both use functions that are ‘hooked’ into  the WP core functions• Fu...
Calling Actions...• Actions are called using do_action ie:  do_action (‘mypluginactions’,$var1,$var2);This simply “tells” ...
Anatomy of an Action• Hooking the action is done using  add_action($tag,$function,$priority,$args)$tag – is the action you...
Adding Actions<?phpfunction myfunction($var1=null, $var2=null) {      echo $var1.’<br>’;      echo $var2,’<br>’;}add_actio...
Actions: Some Uses• Loading Scripts & StyleSheets• Recording user access (login & logout)• Building Child Themes  – Adding...
Hooking filters• apply_filters actually calls the filter function  to APPLY it.  $var1 = apply_filters(‘mypluginfilter’,$v...
Anatomy of Filter• Creating the filter is done using  add_filter($tag,$function,$priority,$args)$tag – is the filter you w...
Example Filter function<?phpfunction myfilter($var1, $var2=null) {        if ($var2==null) {                 $var1 = ‘This...
Filters: Some Uses• Modifying ‘the content’  – Remove shortcodes  – Strip tags / html  – Add elements• Add custom classes ...
HINT  You can add actions and filters to your   plugins to make them more extendableSome examples:• WooCommerce• WP E Comm...
Other stuff to consider• Namespace  – Create unique functions name! Use a prefix or suffix    or, better yet, use a CLASS!...
Useful Resources• Wordpress API  http://codex.wordpress.org/Plugin_API• Wordpress Action Reference http://codex.wordpress....
Any Questions?http://facebook.com/askcharlyleethamhttp://facebook.com/charlyleethamhttp://twitter.com/charlyjlhttp://linke...
CODE EXAMPLES...
Loading Scripts• Enqueue Scripts (wp_enqueue_scripts)Wrong:<script type=“text/javascript” src=http://mydomain.com/path/scr...
Loading StyleSheets• Enqueue StylesWrong:<link type=“text/css” href=“http://mydomain.com/path/style.css”>Right:<?phpfuncti...
Recording user access• wp_login<?phpfunction my_login_function($login) {        $user = get_userdatabylogin($login);      ...
Filter Example$var1 = ‘My Title’;$var2 = ‘>>>>’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return...
Filter Example$var1 = ‘My Title’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return:This is My Title
Upcoming SlideShare
Loading in …5
×

Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012

1,542 views

Published on

Many developers know how to write code in php but that doesn't mean they can write code for Wordpress.

When developing sites for clients in Wordpress, it sometimes preferably to write a custom function that will simplify data input, management and display.

The Wordpress coding framework is continually evolving and provides a number of hooks (through actions & filters) to allow developers to quickly and easily grab functions to create world class websites.

Charly will speak about the power of hooks and filters and explain some of the most common ones to use if you want to write your own custom functions, to make the management of your wordpress websites a breeze.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012

  1. 1. Stop Hacking Wordpress Start Working With It! Charly Leetham www.AskCharlyLeetham.com
  2. 2. Share This... @charlyjl charlyleetham
  3. 3. What will we cover• What is the Wordpress API• What are Filters, Actions & Hooks• Creating & Calling Actions• Actions Example• Creating & Applying Filters• Filters Example• Useful Resources
  4. 4. The API...• Wordpress API is designed for anyone to extend the base WP Functionality• Create Plugins or Themes• Integrate with 3rd Party Apps – data in / out• Covers all the functions available to us• Today, looking at a small part of the API, Filters & Actions... Generically known as Hooks...
  5. 5. Filters...• Modify content.... That’s it!• For example: – Change post / page content – Change widget titles – RSS Feeds – Image details• Used by plugins to allow content to be modified without changing core code
  6. 6. Actions• Do something ... when called...• Allow the ‘execution’ of functions at fixed points in the code• For example – After Login – After Logout – When a widget is displayed – When the theme is setup...
  7. 7. • Filters & Actions are basically the same thing...• Both use functions that are ‘hooked’ into the WP core functions• Functions we write can use / provide actions and filters as well!
  8. 8. Calling Actions...• Actions are called using do_action ie: do_action (‘mypluginactions’,$var1,$var2);This simply “tells” wordpress to find all functionsthat are ‘hooked’ to this action and run them.It also specifies any arguments that may bepassed to the function – in this case two: $var1,$var2 – or there may be none.
  9. 9. Anatomy of an Action• Hooking the action is done using add_action($tag,$function,$priority,$args)$tag – is the action you want to ‘hook to’$function – the function you want to ‘hook’.$priority – the order. 10 is default, 1 is thehighest.$args – the number of arguments to pass tothe action
  10. 10. Adding Actions<?phpfunction myfunction($var1=null, $var2=null) { echo $var1.’<br>’; echo $var2,’<br>’;}add_action(‘mypluginactions’,’myfunction’,10,2);?>This will run ‘myfunction’, when the action‘mypluginactions’ is called and pass 2 arguments.
  11. 11. Actions: Some Uses• Loading Scripts & StyleSheets• Recording user access (login & logout)• Building Child Themes – Adding / removing functionality • Author Bio on posts • “Share This”• Adding / Saving Meta Data for Posts• Processing Ajax Requests
  12. 12. Hooking filters• apply_filters actually calls the filter function to APPLY it. $var1 = apply_filters(‘mypluginfilter’,$var1,$var2);• NOTE: – $var1 MUST be specified and be initialised – i.e $var1 must exist when applying the filter – apply_filters returns a value, so assign the result to a variable.
  13. 13. Anatomy of Filter• Creating the filter is done using add_filter($tag,$function,$priority,$args)$tag – is the filter you want to ‘hook to’$function – the function you want to ‘hook’.$priority – the order. 10 is default, 1 is thehighest.$args – the number of arguments to pass tothe filter
  14. 14. Example Filter function<?phpfunction myfilter($var1, $var2=null) { if ($var2==null) { $var1 = ‘This is ‘.$var1; } else { $var1 =$var2.$var1; } return $var1;}add_filter(‘mypluginfilter’,’myfilter’,10,2)?>This will run ‘myfilter’, when the filter ‘mypluginfilter’ is applied,pass 2 arguments & return $var1 to be displayed / used.
  15. 15. Filters: Some Uses• Modifying ‘the content’ – Remove shortcodes – Strip tags / html – Add elements• Add custom classes to the Body and Post tags• Processing Shortcodes in Widgets – add_filter(‘widget_text’,’do_shortcode’);• Changing Widget Titles• Adding functions to WP Nav Menu’s
  16. 16. HINT You can add actions and filters to your plugins to make them more extendableSome examples:• WooCommerce• WP E Commerce• Gravity Forms• Duplicate Post
  17. 17. Other stuff to consider• Namespace – Create unique functions name! Use a prefix or suffix or, better yet, use a CLASS!• Only load functions when needed – Become familiar with conditional functions to keep your load time tight• Security! – Make use of Wordpress & PHP functions for security: • esc_html(), esc_attr(), esc_js(), esc_textarea() esc_url(), wp_nonce_field(), wp_verify_nonce()
  18. 18. Useful Resources• Wordpress API http://codex.wordpress.org/Plugin_API• Wordpress Action Reference http://codex.wordpress.org/Plugin_API/Action_Reference• Wordpress Filter Reference http://codex.wordpress.org/Plugin_API/Filter_Reference• PHP Xref for Wordpress http://phpxref.com/xref/wordpress/
  19. 19. Any Questions?http://facebook.com/askcharlyleethamhttp://facebook.com/charlyleethamhttp://twitter.com/charlyjlhttp://linkedin.com/in/charlyleethamhttp://www.youtube.com/user/AskCharlyLeethamcharlyjltickets@askcharlyleetham.com www.AskCharlyLeetham.com
  20. 20. CODE EXAMPLES...
  21. 21. Loading Scripts• Enqueue Scripts (wp_enqueue_scripts)Wrong:<script type=“text/javascript” src=http://mydomain.com/path/script.js>Right:<?phpfunction add_my_script() {if (!ismypage) { return; } $src = get_stylesheet_directory_uri().’/path/myscript.js’; wp_enqueue_script(‘myscript‘,$src,array(‘jquery’),’1.0’,true );}add_action(wp_enqueue_scripts, ‘add_my_script‘,10);?>
  22. 22. Loading StyleSheets• Enqueue StylesWrong:<link type=“text/css” href=“http://mydomain.com/path/style.css”>Right:<?phpfunction add_my_style() {If (!ismypage) { return; } $src = get_stylesheet_directory_uri().’/path/style.css’; $dep = get_stylesheet_directory_uri().’/path/layout.css’; wp_enqueue_style(‘mystyle‘,$src,$dep,’1.0’,’screen’ );}add_action(wp_enqueue_scripts, ‘add_my_style‘,10);?>
  23. 23. Recording user access• wp_login<?phpfunction my_login_function($login) { $user = get_userdatabylogin($login); $userid=$user->ID; //code to record login details}add_action(wp_login, ‘my_login_function’, 10,1);
  24. 24. Filter Example$var1 = ‘My Title’;$var2 = ‘>>>>’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return:>>>> My Title
  25. 25. Filter Example$var1 = ‘My Title’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return:This is My Title

×