Hooked on WordPress     Actions and Filters
John Dillick              @johndillickCore developer on Shopp e-commerce pluginfor WordPressDeveloping for and supporting ...
Who’s this for?Desire to extend and customize WordPressSome PHP knowledgeNot already a WordPress plugin developer, orjust ...
WTF are actions/filtersRTFM - see http://codex.wordpress.org/Plugin_APIActions are points of execution in WordPressthat you...
What actions/filter are     out there?Short answer: Too many to cover in 45minutes.Covering some important ones; Dig into c...
Actions, why?Devs think, ‘wouldn’t it be nice to have “this”happen when “that” happens?’One way to do this: modify WordPre...
What does an action    “hook” look like?   <?php do_action(‘action_name’); ?>                 <?phpdo_action_ref_array(‘ac...
How to hook in:If the “hook” name in WP is ‘do_this’:<?php do_action(‘do_this’); ?>You hook in by providing a “callback” (...
CallbacksQuick PHP review, a callback is:1. String name of a function: ‘funk’<?php function funk () {} ?><?php//callbackad...
Callbacks, cont.2. An array with an Object and a string functionname.<?phpclass A {    function funk () {}}$A = new A();//...
Callbacks, cont.3. An array with a class name and a string staticfunction name.<?phpclass B {     static function funk () ...
Callbacks, cont.3. Callback returned from a function, such ascreate_function()<?php// callback for anonymous function$my_f...
add_action()The full form:<?phpadd_action( $tagname, $callback, $priority,$argcount );?>
add_action() cont.$priority: (Default 10) The smaller the number,the quicker your function will happen, thebigger, the lat...
do_action() example<?php //your codeadd_action(‘hello_set’,’a’,10,1);add_action(‘hello_set’,’b’,1);function a ( &$hello ) ...
What output?Answer: NothingBecause function ‘b’ was called with a higherpriority, and exited, ‘a’ never ran... nor anythin...
FilteringWordPress is using some data to dosomething, for instance, to output the body ofa post on my blog.As a developer,...
How to filter:If the “filter” tag name in WP is ‘the_content’:<?php apply_filters(‘the_content’,$postcontent); ?>You hook in ...
Full filter syntaxadd_filter($tagname, $callback, $priority,$num_of_args);Your filter is expected to return something.Forgett...
Wordpress with ‘P’!Back in 2010, WordPress decided to takemeasures to ensure the WordPress trademark wasused properly.<?ph...
If I had core commit...... you would all know.<?phpfunction john_dillick_rocks ( $content ) {!    $content = str_replace(‘...
Removing Action/Filterremove_action($tag,$callback,$priority,$nargs);remove_filter($tag,$callback,$priority,$nargs);remove_...
Check for an action/filterhas_action($tag,$callback);has_filter($tag,$callback);<?phpfunction enlightened () {!    if ( has_...
Common Actionsadd_action(‘init’,$callback);Early action for plugin initialization.add_action(‘admin_init’,$callback);Calle...
Common Actionsadd_action(‘wp_head’,$callback);Called in theme when page header is output.add_action(‘admin_head’,$callback...
Action Referencehttp://codex.wordpress.org/Plugin_API/Action_Reference
Common Filtersadd_filter(‘the_content’,$callback);Filters post and page content.add_filter(‘the_excerpt’,$callback);Filters ...
Common Filtersadd_filter(‘the_title’,$callback);Filters post and page content titles.add_filter(‘wp_title’,$callback);Filter...
Filters Referencehttp://codex.wordpress.org/Plugin_API/Filter_Reference
Upcoming SlideShare
Loading in …5
×

Actions filters

1,209 views

Published on

Introduction to actions and filters.

Published in: Technology, Business
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
1,209
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
9
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide
  • \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
  • \n
  • \n
  • \n
  • \n
  • Actions filters

    1. 1. Hooked on WordPress Actions and Filters
    2. 2. John Dillick @johndillickCore developer on Shopp e-commerce pluginfor WordPressDeveloping for and supporting WordPress for3 yearsTechnical writer and information sponge.Not a design guy; Leave graphics and designto other guys.
    3. 3. Who’s this for?Desire to extend and customize WordPressSome PHP knowledgeNot already a WordPress plugin developer, orjust need a place to sit and zone out for 45minutes.Don’t mind hearing me say “Um” at least athousand times. I’m not a professionalspeaker.
    4. 4. WTF are actions/filtersRTFM - see http://codex.wordpress.org/Plugin_APIActions are points of execution in WordPressthat your plugin can “hook” code to execute.Filters give your code an opportunity to add/remove/modify data before it will be used byWordPress and other plugins.
    5. 5. What actions/filter are out there?Short answer: Too many to cover in 45minutes.Covering some important ones; Dig into codexand WordPress code itself to discoveramazing possibilities.Covering what the hooks look like, syntaxstuff, and how to leverage them.
    6. 6. Actions, why?Devs think, ‘wouldn’t it be nice to have “this”happen when “that” happens?’One way to do this: modify WordPress codedirectly. NO!Actions are provided by the developer of aparticular feature of WordPress to give you anopportunity to do “something” too.Often provide you an opportunity to put thingsinto motion before, during, or after anotheraction
    7. 7. What does an action “hook” look like? <?php do_action(‘action_name’); ?> <?phpdo_action_ref_array(‘action_name’,$args); ?>
    8. 8. How to hook in:If the “hook” name in WP is ‘do_this’:<?php do_action(‘do_this’); ?>You hook in by providing a “callback” (functionname):<?php add_action(‘do_this’,‘my_function’); ?>
    9. 9. CallbacksQuick PHP review, a callback is:1. String name of a function: ‘funk’<?php function funk () {} ?><?php//callbackadd_action(‘do_something’,‘funk’);
    10. 10. Callbacks, cont.2. An array with an Object and a string functionname.<?phpclass A { function funk () {}}$A = new A();//callback for $A->funk();add_action( ‘do_something’, array($A,‘funk’) ); ?>
    11. 11. Callbacks, cont.3. An array with a class name and a string staticfunction name.<?phpclass B { static function funk () {}}//callback for B::funk();add_action( ‘do_something’, array(‘B’,‘funk’) );?>
    12. 12. Callbacks, cont.3. Callback returned from a function, such ascreate_function()<?php// callback for anonymous function$my_func = create_function(“$args”,”echo ‘hello world!’;”);add_action( ‘do_something’, $my_func );?>
    13. 13. add_action()The full form:<?phpadd_action( $tagname, $callback, $priority,$argcount );?>
    14. 14. add_action() cont.$priority: (Default 10) The smaller the number,the quicker your function will happen, thebigger, the later.$argcount: (Default 1) If the action passes 1 ormore arguments as useful data, you can tellhow many you want passed to your callback.
    15. 15. do_action() example<?php //your codeadd_action(‘hello_set’,’a’,10,1);add_action(‘hello_set’,’b’,1);function a ( &$hello ) { $hello = ‘hola, el mundo’; }function b () { exit(); } ?><?php// Primitive action hook, pretend it’s in WordPress$hello = ‘hello world’;do_action(‘hello_set’, $hello);echo $hello;?>
    16. 16. What output?Answer: NothingBecause function ‘b’ was called with a higherpriority, and exited, ‘a’ never ran... nor anythingelse afterwards.All plugins have an opportunity to break all otherplugins at some point.Use standard WordPress function calls wheneverpossible.
    17. 17. FilteringWordPress is using some data to dosomething, for instance, to output the body ofa post on my blog.As a developer, I want to be able to add,remove, or modify that data before WordPressuses it.
    18. 18. How to filter:If the “filter” tag name in WP is ‘the_content’:<?php apply_filters(‘the_content’,$postcontent); ?>You hook in by providing a “callback”, just like withactions hooks.<?php add_filter(‘the_content’,‘my_function’); ?>my_function will always expect at least one arg,and will always return an arg.
    19. 19. Full filter syntaxadd_filter($tagname, $callback, $priority,$num_of_args);Your filter is expected to return something.Forgetting to return something will certainlybreak something.
    20. 20. Wordpress with ‘P’!Back in 2010, WordPress decided to takemeasures to ensure the WordPress trademark wasused properly.<?phpadd_filter(‘the_content’,‘capital_P_dangit’);add_filter(‘the_title’,‘capital_P_dangit’);add_filter(‘comment_text’,‘capital_P_dangit’);?>
    21. 21. If I had core commit...... you would all know.<?phpfunction john_dillick_rocks ( $content ) {! $content = str_replace(‘Dillick sucks’,‘Dillick rocks’,$content);! return $content . ‘ - BTW, you know John Dillick rocks, right?’;}add_filter(‘the_content’,‘john_dillick_rocks’);add_filter(‘the_title’,‘john_dillick_rocks’);add_filter(‘comment_text’,‘john_dillick_rocks’);?>
    22. 22. Removing Action/Filterremove_action($tag,$callback,$priority,$nargs);remove_filter($tag,$callback,$priority,$nargs);remove_all_actions($tag,$priority);remove_all_filters($tag,$priority);<?phpforeach ( array(‘the_content’,‘the_title’,‘comment_text’) as $tag ) {! remove_action($tag,’capital_P_dangit’);! // remove_action($tag,‘john_dillick_rocks’);}
    23. 23. Check for an action/filterhas_action($tag,$callback);has_filter($tag,$callback);<?phpfunction enlightened () {! if ( has_filter(‘the_content’,‘john_dillick_rocks’) )! ! echo “<p>This site powered by Sasquatch.</p>”;! else echo “<p>Nothing to see here. Move along.</p>”;}add_action(‘wp_footer’,’enlightened’);?>
    24. 24. Common Actionsadd_action(‘init’,$callback);Early action for plugin initialization.add_action(‘admin_init’,$callback);Called on admin initialization.
    25. 25. Common Actionsadd_action(‘wp_head’,$callback);Called in theme when page header is output.add_action(‘admin_head’,$callback);Called in admin when page header is output.add_action(‘wp_footer’,$callback);Called in theme when page footer is output.add_action(‘admin_footer’,$callback);Called in admin when page footer is output.
    26. 26. Action Referencehttp://codex.wordpress.org/Plugin_API/Action_Reference
    27. 27. Common Filtersadd_filter(‘the_content’,$callback);Filters post and page content.add_filter(‘the_excerpt’,$callback);Filters post and page excerpt content.
    28. 28. Common Filtersadd_filter(‘the_title’,$callback);Filters post and page content titles.add_filter(‘wp_title’,$callback);Filters blog page title.
    29. 29. Filters Referencehttp://codex.wordpress.org/Plugin_API/Filter_Reference

    ×