Your SlideShare is downloading. ×
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

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


Published on

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

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

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Stop Hacking Wordpress Start Working With It! Charly Leetham
  • 2. Share This... @charlyjl charlyleetham
  • 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. 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. 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. 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. • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Useful Resources• Wordpress API• Wordpress Action Reference• Wordpress Filter Reference• PHP Xref for Wordpress
  • 19. Any Questions?
  • 20. CODE EXAMPLES...
  • 21. Loading Scripts• Enqueue Scripts (wp_enqueue_scripts)Wrong:<script type=“text/javascript” src=>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. Loading StyleSheets• Enqueue StylesWrong:<link type=“text/css” href=“”>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. 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. Filter Example$var1 = ‘My Title’;$var2 = ‘>>>>’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return:>>>> My Title
  • 25. Filter Example$var1 = ‘My Title’;$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);echo $var1;Will return:This is My Title