WP COS Meetup
How to create your own
WordPress plugin.
10/20/15
What is a Plugin?
"Plugins are tools to extend the functionality of WordPress.“
Essentially, they are just a collection of functions that can be activated
and deactivated together.
Almost all plugins use hooks to get things done.
Filter Hooks
Filters are the hooks that WordPress fires to modify
text of various types before adding it to the database or
sending it to the browser screen.
Action Hooks
Actions are the hooks that the WordPress fires at
specific points during execution, or when specific events
occur.
How do Plugins work?
Common Hooks?
Actions
init
template_redirect
admin_init
after_setup_theme
wp_loaded
wp_head
wp_footer
admin_menu
Filters
the_content
the_title
wp_title
template_include
body_class
post_class
How are hooks fired?
To fire an action:
• do_action( 'action_name' );
• - This fires all functions hooked into that action
To fire a filter:
• apply_filters( 'filter_name', 'Filter value' );
• - The 'Filter value' will be the first argument of each hooked function apply_filters will return the filtered input
Responding to hooks?
Actions:
• add_action( 'action_name', 'func_name' );
Filters:
• add_filter( 'filter_name', 'function_name' );
Both take two optional arguments:
• Priority
• Accepted Arguments
What’s this priority thing?
Actions and filters are executed in
the order they are added. So this
code will echo:
First
Second
Third
How does priority work?
Priority changes this order.
Actions and filters have a
default priority of 10. Using a
lower number gives it higher
priority. This code ouputs:
Third
First
Second
Accepted Arguments
Accepted arguments allows you to specify how many arguments are sent to a function
Defaults to 1 for both actions and filters
The following would output:
Don't mention Joomla
Don't mention
One more way to hook into WordPress is through
shortcodes
• Shortcodes are inserted into a post or page from the editor;
• WordPress replaces the shortcode tags with dynamically generated content
Shortcodes are kind of like HTML:
[foo bar='bar']
[foo bar='bar']Some Content[/foo]
We won’t be going over shortcodes.
But Wait! There’s more!
Let’s get started
We are going to build a build a plugin that adds some text below the menu bar.
We will use the Genesis Sample theme because it has a great visual hook
guide that allows you to see the hooks on the page.
I have also installed the Genesis Visual Hook Guide Plugin which shows
the hooks in your Genesis child theme.
Not all themes will have the hook we will be using.
Example from Genesis Theme
/*
Plugin Name: WP Plugin Example #1
Plugin URI: http://tgtech.com/plugins/
Description: Plugin example for Colorado Springs WP Meetup
Author: John Tighe
Author URI: http://tgtech.com
Version: 1.0
*/
Plugin Header Requirements
Folder Structure
Live Example
We are going to use a modified version of Pippin Williamson’s
My First WordPress Plugin
https://pippinsplugins.com/how-to-begin-writing-your-first-wordpress-plugin/
WordPress Meetup
Questions or Comments?
Acknowledgements
This presentation could not have been possible without the work of others.
Pippin Williamson’s
- My First WordPress Plugin Tutorial
(https://pippinsplugins.com/how-to-begin-writing-your-first-wordpress-plugin/ )
John Bloch
- WordPress plugin development for beginners
(http://www.slideshare.net/johnpbloch/wordpress-plugin-development-for-beginners )
Christos Zigkolis
- Wordpress Plugin Development Short Tutorial
(http://www.slideshare.net/chzigkol/wordpress-plugin-development-short-tutorial-presentation )
… And anyone I may have failed to mention
Anthony Montalbano
- Write your first WordPress plugin
(http://www.slideshare.net/italianst4/write-your-first-word-press-plugin-13477027)

How to create your own WordPress plugin

  • 1.
    WP COS Meetup Howto create your own WordPress plugin. 10/20/15
  • 2.
    What is aPlugin? "Plugins are tools to extend the functionality of WordPress.“ Essentially, they are just a collection of functions that can be activated and deactivated together.
  • 3.
    Almost all pluginsuse hooks to get things done. Filter Hooks Filters are the hooks that WordPress fires to modify text of various types before adding it to the database or sending it to the browser screen. Action Hooks Actions are the hooks that the WordPress fires at specific points during execution, or when specific events occur. How do Plugins work?
  • 4.
  • 5.
    How are hooksfired? To fire an action: • do_action( 'action_name' ); • - This fires all functions hooked into that action To fire a filter: • apply_filters( 'filter_name', 'Filter value' ); • - The 'Filter value' will be the first argument of each hooked function apply_filters will return the filtered input
  • 6.
    Responding to hooks? Actions: •add_action( 'action_name', 'func_name' ); Filters: • add_filter( 'filter_name', 'function_name' ); Both take two optional arguments: • Priority • Accepted Arguments
  • 7.
    What’s this prioritything? Actions and filters are executed in the order they are added. So this code will echo: First Second Third
  • 8.
    How does prioritywork? Priority changes this order. Actions and filters have a default priority of 10. Using a lower number gives it higher priority. This code ouputs: Third First Second
  • 9.
    Accepted Arguments Accepted argumentsallows you to specify how many arguments are sent to a function Defaults to 1 for both actions and filters The following would output: Don't mention Joomla Don't mention
  • 10.
    One more wayto hook into WordPress is through shortcodes • Shortcodes are inserted into a post or page from the editor; • WordPress replaces the shortcode tags with dynamically generated content Shortcodes are kind of like HTML: [foo bar='bar'] [foo bar='bar']Some Content[/foo] We won’t be going over shortcodes. But Wait! There’s more!
  • 11.
    Let’s get started Weare going to build a build a plugin that adds some text below the menu bar. We will use the Genesis Sample theme because it has a great visual hook guide that allows you to see the hooks on the page. I have also installed the Genesis Visual Hook Guide Plugin which shows the hooks in your Genesis child theme. Not all themes will have the hook we will be using.
  • 12.
  • 13.
    /* Plugin Name: WPPlugin Example #1 Plugin URI: http://tgtech.com/plugins/ Description: Plugin example for Colorado Springs WP Meetup Author: John Tighe Author URI: http://tgtech.com Version: 1.0 */ Plugin Header Requirements
  • 14.
  • 15.
    Live Example We aregoing to use a modified version of Pippin Williamson’s My First WordPress Plugin https://pippinsplugins.com/how-to-begin-writing-your-first-wordpress-plugin/
  • 16.
  • 17.
    Acknowledgements This presentation couldnot have been possible without the work of others. Pippin Williamson’s - My First WordPress Plugin Tutorial (https://pippinsplugins.com/how-to-begin-writing-your-first-wordpress-plugin/ ) John Bloch - WordPress plugin development for beginners (http://www.slideshare.net/johnpbloch/wordpress-plugin-development-for-beginners ) Christos Zigkolis - Wordpress Plugin Development Short Tutorial (http://www.slideshare.net/chzigkol/wordpress-plugin-development-short-tutorial-presentation ) … And anyone I may have failed to mention Anthony Montalbano - Write your first WordPress plugin (http://www.slideshare.net/italianst4/write-your-first-word-press-plugin-13477027)