Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019

1,143 views

Published on

Hooking is a technique which is used to register an event and to modify a program's behavior when that event occurs, without modifying the original program/code.

WordPress has this technique implemented and by using this you can customise it according to your convenience.

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019

  1. 1. Hooking with WordPress
  2. 2. What is Hooking?
  3. 3. Example:
  4. 4. Example:
  5. 5. Methods - Source Modification - Modify behavior before the application starts. - i.e, patch created by reverse engineering the application - Runtime Modification - Modify behavior while the application is running.
  6. 6. Core php example
  7. 7. Hooks <?php function do_action( $function_name, $args ) { if ( function_exists( $function_name ) { call_user_func( $function_name, $args ); } } ?> <?php function apply_filter( $function_name, $args ) { if ( function_exists( $function_name ) { return call_user_func( $function_name, $args ); } return $args; } ?>
  8. 8. WordPress CMS
  9. 9. WordPress Hooks - Actions - Execute extra code in between code execution. - Filters - Change/Update behaviour of existing code.
  10. 10. WP Action <?php do_action( ‘init’); <?php function wporg_custom() { // do something } add_action('init', 'wporg_custom'); add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] ); Examples:
  11. 11. WP Filters <?php $title = apply_filter( ‘the_title’, $title ); <?php function wporg_filter_title($title) { return 'The ' . $title . ' was filtered'; } add_filter('the_title', 'wporg_filter_title'); add_filter ( 'hook_name', 'your_filter', [priority], [accepted_args] ); Examples:
  12. 12. Hooking: https://en.wikipedia.org/wiki/Hooking WP Hooks: https://developer.wordpress.org/plugins/hooks/ WP Actions: https://developer.wordpress.org/plugins/hooks/actions/ WP Filter: https://developer.wordpress.org/plugins/hooks/filters/ References
  13. 13. WP Reference Code https://core.trac.wordpress.org/browser/trunk/src/wp- includes/plugin.php#L428 https://core.trac.wordpress.org/browser/trunk/src/wp- includes/class-wp-hook.php
  14. 14. Got Questions? Rahul Prajapati @rahulsprajapati rahul.prajapati@live.in

×