SlideShare a Scribd company logo
Develop the skills of planning and developing your plugins
with emphasis on hooks, callback functions and more!
About Bruce Chamoff
 CEO of Hot Web Ideas, Inc, hotwebideas.net, a New York based
web development firm of over 20 years with a portfolio of over
1,000 websites including Korg, JP Morgan Chase, Whirlpool, UBS
 A WordPress theme and plugin developer of 9 years
 Instructor on 5 Udemy.com WordPress Development Courses
 A contributor to the WordPress.org development forums
 Speaker at WordCamps.
 Dad with one daughter.
What Exactly Is a Plugin?
 It is a PHP script that extends WordPress to perform new
functions out of the box.
 Allows developers to change the WordPress core without
modifying any of its code.
 Contains a combination of PHP, HTML, CSS, and Javascript.
 The PHP code of any plugin consists of a combination of one
or more WordPress hooks and one or more callback
functions.
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Why Do We Need Plugins?*
 To prevent hacking the WordPress core
 To protect our code from WordPress updates
 To extend the basic WordPress installation and allow it
to perform new functionality
DON’T HACK THE CORE!
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
What Is The WordPress Core?
 All folders and files outside of the wp-content
directory including:
 Root
 WP-includes
 WP-admin
 We CAN edit anything under the WP-Content directory
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Where is the Code for Plugins Stored?
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
How To Name a Plugin
 Since plugins are PHP files, they can have any file name with a
.PHP extension, but preferably identical to its folder name.
 They can be stored in a folder or directly in the wp-content
/plugins directory.
 Examples of plugin folders and names include:
 wp-content /plugins/folder-name/my-plugin.php
 wp-content /plugins/my-plugin.php
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Simple Plugin Plan
 Planning your plugin involves 5 simple questions:
 Who will use your plugin?
 When will your plugin run?
 Where will your plugin run?
 What will execute?
 How will it execute?
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Plugin Architecture & Coding Structure
1. File Header
2. Hook
3. Callback Function
add_filter(‘HOOK NAME’,’CALLBACK FUNCTION REFERENCE’);
or add_action
function CALLBACK FUNCTION ()
{
All PHP content which can include WordPress API code,
wp_content, get_posts.
return data, PHP variable, object, array, string literal, etc;
}
These 2 must match.
More About Hooks
1. There are two types of hooks: action and filter hooks.
A. Action hooks tell WordPress to perform an action.
B. Filter hooks tell WordPress to change some part of the content.
2. Hooks are a necessary part of any plugin as they instruct WordPress to act on a certain
feature or piece of content.
3. They tell WordPress when, where, and for whom to run the code inside the plugin.
4. Themes can also include hooks in their functions.php file.
5. Interesting Fact: Drupal also uses hooks in its modules.
There are hundreds of hooks listed at:
https://codex.wordpress.org/Plugin_API/Filter_Reference
https://codex.wordpress.org/Plugin_API/Action_Reference
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
My Most Frequently Used HooksFilter Hooks:
• the_content
• admin_footer_text
• body_class
• login_headertitle
• wp_footer
• the_modified_time
Action Hooks:
• admin_menu
•rest_api_init
•init
• admin_init
• admin_notices
• wp_dashboard_setup
• admin_bar_menu
• wp_enqueue_scripts
• show_user_profile
• edit_user_profile
• personal_options_update
• edit_user_profile_update
• login_form_login
• widgets_init
• customize_register
• after_setup_theme
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Hook Example:
Announce New FB Page
Where: At the end of every blog post.
When: N/A
Who: Any public user (non-admin role)
Use: the_content
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Hook Example:
Greet an Admin
Where: In the WordPress back end
When: When an admin entered the back end
Who: Someone with the administrator or editor user role
Use: admin_notices
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Hook Example:
Greet an Admin
Where: In the WordPress user editing screen
When: Any user clicks the Edit Profile link
Who: Any user with rights to edit his/her profile.
Use: edit_user_profile
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Hook Example:
Run Javascript Files
Where: Anywhere Javascript is needed
When: Our plugin executes
Who: N/A
Use: wp_enqueue_scripts
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
More About The Callback Function
1. It is a regular PHP function.
2. Besides PHP code, it can contain WordPress API code. See Next Slide
3. The function name must match the callback function’s reference in the
referring hook.
4. The code for callback functions can also be written as object-oriented code
as well.
5. Depending on the hook calling the function, it can accept any number of
parameters or none.
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
My Most Frequently Used WordPress
APIs in Plugin Callback Functions
1. Options API
2. Transients API
3. Metadata API
4. Theme Customization API (also used in themes known as the customizer)
5. Shortcode API
6. Widgets API
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
Review:
Steps For Developing Our Plugin
1. Open our text editor (Notepad++, Edit Plus, BBEdit, Notepad, etc.)
2. Add the PHP directives.
3. Write our plugin header.
4. Write our hook.
5. Write our callback function.
6. Add supporting files (images, HTML, CSS, Javascript)
7. Save our plugin as a PHP script under wp-
content/plugins/folder-name/plugin-name.php
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
A Simple Plugin:
<?php
/*
Plugin Name: Add My URL
Description: Add any URL to the end of all WordPress posts and pages
Version 1.0
Author: Bruce Chamoff
Author URL: http://www.hotwebideas.net
*/
add_filter(‘the_content’, ’add_my_website’);
function add_my_website($content)
{
$new_content = $content . ‘<p><a href=“http://www.hotwebideas.net”>Visit My Website</a></p>’;
return $new_content;
}
?>
Add Your URL To All Posts and Pages
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
How To Customize This Plugin
1. Look at the slide titled All the code you need or
click this link.
2. Open your favorite text editor and copy the PHP
code to a new file.
3. Save the file as specialid_add_to_the_end.php
where specialid can be any ID you want such as
hwi_add_to_the_end.php
How To Customize This Plugin (continued)
Customize the header:
/*
header
Plugin Name: Add My URL
Description: Add any URL to the end of all WordPress posts and pages
Version 1.0
Author: Bruce Chamoff replace with your name
Author URL: http://www.hotwebideas.net replace with your URL
*/
1. Change the author name
to YOUR name.
2. Change the URL to YOUR
URL or social media page.
How To Customize This Plugin (continued)
Customize the callback function:
//callback function
function add_my_website($content)
{
$new_content = $content . ‘<p><a
href=“http://www.hotwebideas.net”>Visit My Website</a></p>’;
return $new_content;
}
2. Change the call to action to
any thing you want.
1. Change the URL to your new
site or social media page.
Installing Your New Plugin
Now, you can upload it to your WordPress site.
Follow these steps:
1. Save your plugin as a zip file.
2. Log into your WordPress
admin.
3. Click on PluginsAdd New.
4. Continued on next page.
Installing Your New Plugin (Continued)
Click Upload Plugin.
Installing Your New Plugin (Continued)
Click Choose File.
Installing Your New Plugin (Continued)
Navigate to your zip file and double-click it.
Installing Your New Plugin (Continued)
Click Install Now.
Activating Your New Plugin (Continued)
Click Activate Plugin.
Activating Your New Plugin (Continued)
Congratulations! Your new plugin is activated and should now display your new
URL at the end of every blog post!
Contact & Q&A
 Contact Me For Plugin Help
 Twitter @BruceChamoff
 WebDesignerMall.com
 Udemy.com http://udemy.com/user/brucechamoff
 Get 50% Off My WordPress Development courses with
coupon code WORDCAMP
 I will answer any question…
Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff
I teach WordPress Development Courses at http://udemy.com/user/brucechamoff

More Related Content

Introduction To Simple WordPress Plugin Development

  • 1. Develop the skills of planning and developing your plugins with emphasis on hooks, callback functions and more!
  • 2. About Bruce Chamoff  CEO of Hot Web Ideas, Inc, hotwebideas.net, a New York based web development firm of over 20 years with a portfolio of over 1,000 websites including Korg, JP Morgan Chase, Whirlpool, UBS  A WordPress theme and plugin developer of 9 years  Instructor on 5 Udemy.com WordPress Development Courses  A contributor to the WordPress.org development forums  Speaker at WordCamps.  Dad with one daughter.
  • 3. What Exactly Is a Plugin?  It is a PHP script that extends WordPress to perform new functions out of the box.  Allows developers to change the WordPress core without modifying any of its code.  Contains a combination of PHP, HTML, CSS, and Javascript.  The PHP code of any plugin consists of a combination of one or more WordPress hooks and one or more callback functions. Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 4. Why Do We Need Plugins?*  To prevent hacking the WordPress core  To protect our code from WordPress updates  To extend the basic WordPress installation and allow it to perform new functionality DON’T HACK THE CORE! Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 5. What Is The WordPress Core?  All folders and files outside of the wp-content directory including:  Root  WP-includes  WP-admin  We CAN edit anything under the WP-Content directory Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 6. Where is the Code for Plugins Stored? Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 7. How To Name a Plugin  Since plugins are PHP files, they can have any file name with a .PHP extension, but preferably identical to its folder name.  They can be stored in a folder or directly in the wp-content /plugins directory.  Examples of plugin folders and names include:  wp-content /plugins/folder-name/my-plugin.php  wp-content /plugins/my-plugin.php Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 8. Simple Plugin Plan  Planning your plugin involves 5 simple questions:  Who will use your plugin?  When will your plugin run?  Where will your plugin run?  What will execute?  How will it execute? Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 9. Plugin Architecture & Coding Structure 1. File Header 2. Hook 3. Callback Function add_filter(‘HOOK NAME’,’CALLBACK FUNCTION REFERENCE’); or add_action function CALLBACK FUNCTION () { All PHP content which can include WordPress API code, wp_content, get_posts. return data, PHP variable, object, array, string literal, etc; } These 2 must match.
  • 10. More About Hooks 1. There are two types of hooks: action and filter hooks. A. Action hooks tell WordPress to perform an action. B. Filter hooks tell WordPress to change some part of the content. 2. Hooks are a necessary part of any plugin as they instruct WordPress to act on a certain feature or piece of content. 3. They tell WordPress when, where, and for whom to run the code inside the plugin. 4. Themes can also include hooks in their functions.php file. 5. Interesting Fact: Drupal also uses hooks in its modules. There are hundreds of hooks listed at: https://codex.wordpress.org/Plugin_API/Filter_Reference https://codex.wordpress.org/Plugin_API/Action_Reference Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 11. My Most Frequently Used HooksFilter Hooks: • the_content • admin_footer_text • body_class • login_headertitle • wp_footer • the_modified_time Action Hooks: • admin_menu •rest_api_init •init • admin_init • admin_notices • wp_dashboard_setup • admin_bar_menu • wp_enqueue_scripts • show_user_profile • edit_user_profile • personal_options_update • edit_user_profile_update • login_form_login • widgets_init • customize_register • after_setup_theme Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 12. Hook Example: Announce New FB Page Where: At the end of every blog post. When: N/A Who: Any public user (non-admin role) Use: the_content Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 13. Hook Example: Greet an Admin Where: In the WordPress back end When: When an admin entered the back end Who: Someone with the administrator or editor user role Use: admin_notices Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 14. Hook Example: Greet an Admin Where: In the WordPress user editing screen When: Any user clicks the Edit Profile link Who: Any user with rights to edit his/her profile. Use: edit_user_profile Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 15. Hook Example: Run Javascript Files Where: Anywhere Javascript is needed When: Our plugin executes Who: N/A Use: wp_enqueue_scripts Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 16. More About The Callback Function 1. It is a regular PHP function. 2. Besides PHP code, it can contain WordPress API code. See Next Slide 3. The function name must match the callback function’s reference in the referring hook. 4. The code for callback functions can also be written as object-oriented code as well. 5. Depending on the hook calling the function, it can accept any number of parameters or none. Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 17. My Most Frequently Used WordPress APIs in Plugin Callback Functions 1. Options API 2. Transients API 3. Metadata API 4. Theme Customization API (also used in themes known as the customizer) 5. Shortcode API 6. Widgets API Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 18. Review: Steps For Developing Our Plugin 1. Open our text editor (Notepad++, Edit Plus, BBEdit, Notepad, etc.) 2. Add the PHP directives. 3. Write our plugin header. 4. Write our hook. 5. Write our callback function. 6. Add supporting files (images, HTML, CSS, Javascript) 7. Save our plugin as a PHP script under wp- content/plugins/folder-name/plugin-name.php Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 19. A Simple Plugin: <?php /* Plugin Name: Add My URL Description: Add any URL to the end of all WordPress posts and pages Version 1.0 Author: Bruce Chamoff Author URL: http://www.hotwebideas.net */ add_filter(‘the_content’, ’add_my_website’); function add_my_website($content) { $new_content = $content . ‘<p><a href=“http://www.hotwebideas.net”>Visit My Website</a></p>’; return $new_content; } ?> Add Your URL To All Posts and Pages Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff
  • 20. How To Customize This Plugin 1. Look at the slide titled All the code you need or click this link. 2. Open your favorite text editor and copy the PHP code to a new file. 3. Save the file as specialid_add_to_the_end.php where specialid can be any ID you want such as hwi_add_to_the_end.php
  • 21. How To Customize This Plugin (continued) Customize the header: /* header Plugin Name: Add My URL Description: Add any URL to the end of all WordPress posts and pages Version 1.0 Author: Bruce Chamoff replace with your name Author URL: http://www.hotwebideas.net replace with your URL */ 1. Change the author name to YOUR name. 2. Change the URL to YOUR URL or social media page.
  • 22. How To Customize This Plugin (continued) Customize the callback function: //callback function function add_my_website($content) { $new_content = $content . ‘<p><a href=“http://www.hotwebideas.net”>Visit My Website</a></p>’; return $new_content; } 2. Change the call to action to any thing you want. 1. Change the URL to your new site or social media page.
  • 23. Installing Your New Plugin Now, you can upload it to your WordPress site. Follow these steps: 1. Save your plugin as a zip file. 2. Log into your WordPress admin. 3. Click on PluginsAdd New. 4. Continued on next page.
  • 24. Installing Your New Plugin (Continued) Click Upload Plugin.
  • 25. Installing Your New Plugin (Continued) Click Choose File.
  • 26. Installing Your New Plugin (Continued) Navigate to your zip file and double-click it.
  • 27. Installing Your New Plugin (Continued) Click Install Now.
  • 28. Activating Your New Plugin (Continued) Click Activate Plugin.
  • 29. Activating Your New Plugin (Continued) Congratulations! Your new plugin is activated and should now display your new URL at the end of every blog post!
  • 30. Contact & Q&A  Contact Me For Plugin Help  Twitter @BruceChamoff  WebDesignerMall.com  Udemy.com http://udemy.com/user/brucechamoff  Get 50% Off My WordPress Development courses with coupon code WORDCAMP  I will answer any question… Website: WebDesignerMall.com / WordPress.org: hotwebideas / Twitter:@brucechamoff I teach WordPress Development Courses at http://udemy.com/user/brucechamoff