SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
Write your first
WordPress plugin
Anthony Montalbano          @italianst4

         anthony@ambrdetroit.com
Who is Anthony Montalbano?
Passionate for code
  Bachelor's in Computer Science
Passionate for WordPress
  WordCamp Detroit Organizer

Passionate for open source
  WordPress plugin developer

Passionate for words
  Serial blogger

Passionate for possibilities
  Co-founder of flipfrog and AMBR Detroit
What is a WordPress plugin?
"Plugins are tools to extend the functionality of WordPress."
                                      ~ http://codex.wordpress.org/Plugins




MEGA IMPORTANT!
The Codex

         The online manual for WordPress and a living
         repository for WordPress information and
         documentation.
What can plugins do?
WordPress Plugins by the Numbers

21,214                                                         345,389,937
# of plugins                                                 # of plugin downloads

                                         63
                                 % of users that use
                                 10 plugins or less                  9,783+
                                                                     # of plugin
    12,134,168                                                       developers
 # of downloads of the most popular
    plugin - All in One SEO Pack
Sources:
http://wordpress.org/extend/plugins/
http://digwp.com/2010/01/poll-results-how-many-plugins-do-you-use/
http://www.daveligthart.com/top-1000-wordpress-plugin-authors/
Your WordPress plugin "Google"
Let's make a plugin!


●   Find and replace a word in the title.
●   Show what is being replaced as a sidebar widget.
●   Admin menu to change the find word and replace word.
●   Email me when a new post is published.
Filter Hooks and Action Hooks
WordPress plugins rely on the many hooks
within the system to get things done.
Filter Hooks
Filters are the hooks that WordPress launches 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 core launches at
specific points during execution, or when specific events
occur.
                                  Source: http://codex.wordpress.org/Plugin_API
How Hooks Work
WordPress Plugin API provides you with a set
of PHP functions that allow you to signal your
own functions to be called when that hook is
called.

Filter Example:
add_filter('the_title', function($title) { return '<b>'.
$title. '</b>';})
                        Filters: http://codex.wordpress.org/Plugin_API/Filter_Reference


Action Example:
add_action( 'save_post', 'my_save_post', 10, 2 );
                      Actions: http://codex.wordpress.org/Plugin_API/Action_Reference
How Hooks Work (continued)
Hooks have 4 parameters
● Tag (required)
  ○ This is the WordPress named location where the
     hook takes place.
● Function (required)
  ○ This is the function to be called when the hook is
     executed.
● Priority (optional)
  ○ This determines the order your function is run, the
     lower, the earlier.
● Parameters (optional)
  ○ This is the number of parameters your function takes
Set the Foundation
● Create a new folder in wp-content/plugins
● Create a php file with a plugin header
  comment box
     /*
     Plugin Name: My First Plugin
     Plugin URI: http://wordpress.org/extend/plugins/
     Description: This is a description of a plugin
     Author: Anthony Montalbano
     Version: alpha
     Author URI: http://www.ambrdetroit.com
     */




                         http://codex.wordpress.org/Writing_a_Plugin#File_Headers
Activation and Uninstall
What will your plugin do when it is first
activated?
   ● Create database tables, data, and files
   ● Update database tables, data, and files


What will your plugin do when it is uninstalled?
   ● Delete databases tables, data, files
On Activation
Add the following hook:
register_activation_hook( __FILE__, 'demo_activate' );

Create a new function called 'demo_activate'
 function demo_activate() {
    //do something when the plugin first initializes

 };




             Source: http://codex.wordpress.org/Function_Reference/register_activation_hook
On Uninstall
Create a file called uninstall.php in the root directory.



Add the following code:
 <?php
 if(!defined('WP_UNINSTALL_PLUGIN'))
    exit();

 delete_option('demo_myValue');




             Source: http://codex.wordpress.org/Function_Reference/register_deactivation_hook
Adding a Filter
Add the following filter hook:
add_filter( 'the_title, 'demo_title_change' );

Create a new function called 'demo_title_change'
 function demo_title_change($title) {
    //do something with the title
    str_replace( 'world', 'something', $title);
    return $title;
 };




                        Source: http://codex.wordpress.org/Function_Reference/add_filter
Adding an Action
Add the following filter hook:
add_action( 'publish_post, 'demo_email_me' );

Create a new function called 'demo_title_change'
 function demo_email_me($post_id) {
    wp_mail('anthony@ambrdetroit.com', 'New post!', 'New
 post on my demo blog, go check it out:' . get_bloginfo
 ('url'));
    return $post_id;
 };




                        Source: http://codex.wordpress.org/Function_Reference/add_filter
Adding a Settings Page
First we need add a hook to where the settings
page will show in the admin:
   add_action('admin_menu', 'my_plugin_menu');



Next we need to add a function to define the
menu:
   function my_plugin_menu() {
      add_options_page('Demo Plugin Options', 'Demo
   Plugin', 'manage_options', 'demo-plugin',
   'demo_plugin_options');
   }

                Source: http://codex.wordpress.org/Function_Reference/add_options_page
Adding a Settings Page (continued)
Finally we need to generate the HTML and
functionality of the admin menu:
function demo_plugin_options() {
   //get the option
    $replaceWord = get_option('demo_myValue');

   //save functionality
   if(isset($_REQUEST['demo_update_admin']) && $_REQUEST
['demo_update_admin']) {
       update_option('demo_myValue', $_POST['myValue']);
       $replaceWord = $_POST['myValue'];
       echo "<div id='message' class='updated fade'><p>Demo Plugin
Settings Saved!</p></div>";
   }

    //display the page
    include_once(dirname(__FILE__) . '/demo_admin.php');
}
Adding a Widget
First we need to add a hook to load the widget
on widget initialization:
  add_action( 'widgets_init', 'demo_load_widgets' );



Next, we need to create a function to register
the widget:
  function demo_load_widgets() {
         register_widget( "demo_widget" );
  }




                               Source: http://codex.wordpress.org/Widgets_API
Adding a Widget (continued)
Finally we create the widget by extending the
WordPress Widget class:
  class Demo_Widget extends WP_Widget {

      public function __construct() {
          // widget actual processes
          parent::__construct(
              'demo_widget', // Base ID
              'Demo Widget', // Name
              array( 'description' => __( 'My Little Demo Widget', 'text_domain' ), ) //
  Arrgy
          );
      }

      public function widget( $args, $instance ) {
          // outputs the content of the widget
          extract( $args );
          $replaceWord = get_option('demo_myValue');

          echo $before_widget;
          if ( ! empty( $replaceWord ) )
              echo $before_title . 'My value' . $after_title . $replaceWord;
          echo $after_widget;
      }
  }
Tip 1: Use a plugin prefix
When creating a plugin, create a unique plugin
prefix that can be used for all functions and
variables.

Since there are many plugins, it's important that
your functions and variables don't conflict with
other plugins.
Tip 2: Never use PHP MySQL calls
WordPress has a great database class called
WPDB and makes it very each to plugin to the
WordPress database.
                   http://codex.wordpress.org/Class_Reference/wpdb



For simple name/value pairs you can use
WordPress options
           http://codex.wordpress.org/Function_Reference/add_option
Tip 3: Queuing Scripts and Styles
There are many cases where you may want to
include a javascript or style sheet with your
plugin. WordPress has this functionality built
in. By default WordPress has many scripts
included, such as jQuery.

       http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Tip 4: Use WordPress Admin Styles
The WordPress admin has a style sheet that
should be used when creating admin menus.
The goal is to make your plugin fit seamless
with WordPress.

   http://codex.wordpress.org/User:TECannon/UI_Pattern_and_Style_Guide
Tip 5: Prepare your SQL Statements
WordPress Database class has a function
called prepare(). Use this function to properly
prepare your SQL statements.

                                                  http://codex.wordpress.
 org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
There's a Plugin for That

         WordCamp Detroit 2010




          http://bit.ly/wcdetplugins
Thank you!

  Anthony Montalbano

      @italianst4

anthony@ambrdetroit.com

Contenu connexe

Tendances

Odoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvOdoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvacsone
 
DrupalCon Barcelona 2015
DrupalCon Barcelona 2015DrupalCon Barcelona 2015
DrupalCon Barcelona 2015Daniel Kanchev
 
Why it's dangerous to turn off automatic updates and here's how to do it
Why it's dangerous to turn off automatic updates and here's how to do itWhy it's dangerous to turn off automatic updates and here's how to do it
Why it's dangerous to turn off automatic updates and here's how to do itOnni Hakala
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern ApproachAlessandro Fiore
 
WordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWalter Ebert
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
Higher Order WordPress Security
Higher Order WordPress SecurityHigher Order WordPress Security
Higher Order WordPress SecurityDougal Campbell
 
How to Speed Up Your Joomla! Site
How to Speed Up Your Joomla! SiteHow to Speed Up Your Joomla! Site
How to Speed Up Your Joomla! SiteDaniel Kanchev
 
Hidden Secrets For A Hack-Proof Joomla! Site
Hidden Secrets For A Hack-Proof Joomla! SiteHidden Secrets For A Hack-Proof Joomla! Site
Hidden Secrets For A Hack-Proof Joomla! SiteDaniel Kanchev
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
Using composer with WordPress
Using composer with WordPressUsing composer with WordPress
Using composer with WordPressMicah Wood
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecturepostrational
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011Tareq Hasan
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin developmentLuc De Brouwer
 

Tendances (20)

Odoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvOdoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenv
 
DrupalCon Barcelona 2015
DrupalCon Barcelona 2015DrupalCon Barcelona 2015
DrupalCon Barcelona 2015
 
Why it's dangerous to turn off automatic updates and here's how to do it
Why it's dangerous to turn off automatic updates and here's how to do itWhy it's dangerous to turn off automatic updates and here's how to do it
Why it's dangerous to turn off automatic updates and here's how to do it
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
wp-cli
wp-cliwp-cli
wp-cli
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
 
WordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwalten
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
Higher Order WordPress Security
Higher Order WordPress SecurityHigher Order WordPress Security
Higher Order WordPress Security
 
How to Speed Up Your Joomla! Site
How to Speed Up Your Joomla! SiteHow to Speed Up Your Joomla! Site
How to Speed Up Your Joomla! Site
 
Secure All The Things!
Secure All The Things!Secure All The Things!
Secure All The Things!
 
Hidden Secrets For A Hack-Proof Joomla! Site
Hidden Secrets For A Hack-Proof Joomla! SiteHidden Secrets For A Hack-Proof Joomla! Site
Hidden Secrets For A Hack-Proof Joomla! Site
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
Using composer with WordPress
Using composer with WordPressUsing composer with WordPress
Using composer with WordPress
 
Extending WordPress
Extending WordPressExtending WordPress
Extending WordPress
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
Theming 101
Theming 101Theming 101
Theming 101
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin development
 

Similaire à Write your first WordPress plugin

Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your willTom Jenkins
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017ylefebvre
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress DevelopmentMindfire Solutions
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeRakesh Kushwaha
 
WordPress Plugin Development 201
WordPress Plugin Development 201WordPress Plugin Development 201
WordPress Plugin Development 201ylefebvre
 
Plugging into plugins
Plugging into pluginsPlugging into plugins
Plugging into pluginsJosh Harrison
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginnersjohnpbloch
 
Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practicesdanpastori
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginMainak Goswami
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!David Gibbons
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginAndolasoft Inc
 
Creating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressCreating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressHristo Chakarov
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For BegineersM A Hossain Tonu
 
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Damien Carbery
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Pluginsrandyhoyt
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress PluginAndy Stratton
 
WordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute WorkshopWordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute WorkshopBrendan Sera-Shriar
 

Similaire à Write your first WordPress plugin (20)

Plug in development
Plug in developmentPlug in development
Plug in development
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your will
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress Development
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
 
WordPress Plugin Development 201
WordPress Plugin Development 201WordPress Plugin Development 201
WordPress Plugin Development 201
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 
Plugging into plugins
Plugging into pluginsPlugging into plugins
Plugging into plugins
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginners
 
Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practices
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress Plugin
 
Creating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressCreating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPress
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For Begineers
 
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)Debugging - Figuring it out yourself (WordCamp Dublin 2019)
Debugging - Figuring it out yourself (WordCamp Dublin 2019)
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
WordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute WorkshopWordPress Plugin Development- Rich Media Institute Workshop
WordPress Plugin Development- Rich Media Institute Workshop
 

Plus de Anthony Montalbano

7 tips to better manage client expectations
7 tips to better manage client expectations7 tips to better manage client expectations
7 tips to better manage client expectationsAnthony Montalbano
 
Building a mini-theme with WordPress REST API
Building a mini-theme with WordPress REST APIBuilding a mini-theme with WordPress REST API
Building a mini-theme with WordPress REST APIAnthony Montalbano
 
Making static sites dynamic (with WordPress yo!)
Making static sites dynamic (with WordPress yo!)Making static sites dynamic (with WordPress yo!)
Making static sites dynamic (with WordPress yo!)Anthony Montalbano
 
Building a website with WordPress
Building a website with WordPressBuilding a website with WordPress
Building a website with WordPressAnthony Montalbano
 
Getting Acclimated to WordPress
Getting Acclimated to WordPressGetting Acclimated to WordPress
Getting Acclimated to WordPressAnthony Montalbano
 
Things to think about when starting a startup
Things to think about when starting a startupThings to think about when starting a startup
Things to think about when starting a startupAnthony Montalbano
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckAnthony Montalbano
 
Steve Barman - CSS and WordPress
Steve Barman - CSS and WordPressSteve Barman - CSS and WordPress
Steve Barman - CSS and WordPressAnthony Montalbano
 
The Power of WordPress Plugins
The Power of WordPress PluginsThe Power of WordPress Plugins
The Power of WordPress PluginsAnthony Montalbano
 

Plus de Anthony Montalbano (12)

7 tips to better manage client expectations
7 tips to better manage client expectations7 tips to better manage client expectations
7 tips to better manage client expectations
 
Building a mini-theme with WordPress REST API
Building a mini-theme with WordPress REST APIBuilding a mini-theme with WordPress REST API
Building a mini-theme with WordPress REST API
 
How to Execute Your Idea (v2)
How to Execute Your Idea (v2)How to Execute Your Idea (v2)
How to Execute Your Idea (v2)
 
Making static sites dynamic (with WordPress yo!)
Making static sites dynamic (with WordPress yo!)Making static sites dynamic (with WordPress yo!)
Making static sites dynamic (with WordPress yo!)
 
WordPress REST API
WordPress REST APIWordPress REST API
WordPress REST API
 
Building a website with WordPress
Building a website with WordPressBuilding a website with WordPress
Building a website with WordPress
 
Getting Acclimated to WordPress
Getting Acclimated to WordPressGetting Acclimated to WordPress
Getting Acclimated to WordPress
 
How to Execute your Idea
How to Execute your IdeaHow to Execute your Idea
How to Execute your Idea
 
Things to think about when starting a startup
Things to think about when starting a startupThings to think about when starting a startup
Things to think about when starting a startup
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
 
Steve Barman - CSS and WordPress
Steve Barman - CSS and WordPressSteve Barman - CSS and WordPress
Steve Barman - CSS and WordPress
 
The Power of WordPress Plugins
The Power of WordPress PluginsThe Power of WordPress Plugins
The Power of WordPress Plugins
 

Dernier

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 

Dernier (20)

Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 

Write your first WordPress plugin

  • 1. Write your first WordPress plugin Anthony Montalbano @italianst4 anthony@ambrdetroit.com
  • 2. Who is Anthony Montalbano? Passionate for code Bachelor's in Computer Science Passionate for WordPress WordCamp Detroit Organizer Passionate for open source WordPress plugin developer Passionate for words Serial blogger Passionate for possibilities Co-founder of flipfrog and AMBR Detroit
  • 3. What is a WordPress plugin? "Plugins are tools to extend the functionality of WordPress." ~ http://codex.wordpress.org/Plugins MEGA IMPORTANT! The Codex The online manual for WordPress and a living repository for WordPress information and documentation.
  • 5. WordPress Plugins by the Numbers 21,214 345,389,937 # of plugins # of plugin downloads 63 % of users that use 10 plugins or less 9,783+ # of plugin 12,134,168 developers # of downloads of the most popular plugin - All in One SEO Pack Sources: http://wordpress.org/extend/plugins/ http://digwp.com/2010/01/poll-results-how-many-plugins-do-you-use/ http://www.daveligthart.com/top-1000-wordpress-plugin-authors/
  • 7. Let's make a plugin! ● Find and replace a word in the title. ● Show what is being replaced as a sidebar widget. ● Admin menu to change the find word and replace word. ● Email me when a new post is published.
  • 8. Filter Hooks and Action Hooks WordPress plugins rely on the many hooks within the system to get things done. Filter Hooks Filters are the hooks that WordPress launches 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 core launches at specific points during execution, or when specific events occur. Source: http://codex.wordpress.org/Plugin_API
  • 9. How Hooks Work WordPress Plugin API provides you with a set of PHP functions that allow you to signal your own functions to be called when that hook is called. Filter Example: add_filter('the_title', function($title) { return '<b>'. $title. '</b>';}) Filters: http://codex.wordpress.org/Plugin_API/Filter_Reference Action Example: add_action( 'save_post', 'my_save_post', 10, 2 ); Actions: http://codex.wordpress.org/Plugin_API/Action_Reference
  • 10. How Hooks Work (continued) Hooks have 4 parameters ● Tag (required) ○ This is the WordPress named location where the hook takes place. ● Function (required) ○ This is the function to be called when the hook is executed. ● Priority (optional) ○ This determines the order your function is run, the lower, the earlier. ● Parameters (optional) ○ This is the number of parameters your function takes
  • 11. Set the Foundation ● Create a new folder in wp-content/plugins ● Create a php file with a plugin header comment box /* Plugin Name: My First Plugin Plugin URI: http://wordpress.org/extend/plugins/ Description: This is a description of a plugin Author: Anthony Montalbano Version: alpha Author URI: http://www.ambrdetroit.com */ http://codex.wordpress.org/Writing_a_Plugin#File_Headers
  • 12. Activation and Uninstall What will your plugin do when it is first activated? ● Create database tables, data, and files ● Update database tables, data, and files What will your plugin do when it is uninstalled? ● Delete databases tables, data, files
  • 13. On Activation Add the following hook: register_activation_hook( __FILE__, 'demo_activate' ); Create a new function called 'demo_activate' function demo_activate() { //do something when the plugin first initializes }; Source: http://codex.wordpress.org/Function_Reference/register_activation_hook
  • 14. On Uninstall Create a file called uninstall.php in the root directory. Add the following code: <?php if(!defined('WP_UNINSTALL_PLUGIN')) exit(); delete_option('demo_myValue'); Source: http://codex.wordpress.org/Function_Reference/register_deactivation_hook
  • 15. Adding a Filter Add the following filter hook: add_filter( 'the_title, 'demo_title_change' ); Create a new function called 'demo_title_change' function demo_title_change($title) { //do something with the title str_replace( 'world', 'something', $title); return $title; }; Source: http://codex.wordpress.org/Function_Reference/add_filter
  • 16. Adding an Action Add the following filter hook: add_action( 'publish_post, 'demo_email_me' ); Create a new function called 'demo_title_change' function demo_email_me($post_id) { wp_mail('anthony@ambrdetroit.com', 'New post!', 'New post on my demo blog, go check it out:' . get_bloginfo ('url')); return $post_id; }; Source: http://codex.wordpress.org/Function_Reference/add_filter
  • 17. Adding a Settings Page First we need add a hook to where the settings page will show in the admin: add_action('admin_menu', 'my_plugin_menu'); Next we need to add a function to define the menu: function my_plugin_menu() { add_options_page('Demo Plugin Options', 'Demo Plugin', 'manage_options', 'demo-plugin', 'demo_plugin_options'); } Source: http://codex.wordpress.org/Function_Reference/add_options_page
  • 18. Adding a Settings Page (continued) Finally we need to generate the HTML and functionality of the admin menu: function demo_plugin_options() { //get the option $replaceWord = get_option('demo_myValue'); //save functionality if(isset($_REQUEST['demo_update_admin']) && $_REQUEST ['demo_update_admin']) { update_option('demo_myValue', $_POST['myValue']); $replaceWord = $_POST['myValue']; echo "<div id='message' class='updated fade'><p>Demo Plugin Settings Saved!</p></div>"; } //display the page include_once(dirname(__FILE__) . '/demo_admin.php'); }
  • 19. Adding a Widget First we need to add a hook to load the widget on widget initialization: add_action( 'widgets_init', 'demo_load_widgets' ); Next, we need to create a function to register the widget: function demo_load_widgets() { register_widget( "demo_widget" ); } Source: http://codex.wordpress.org/Widgets_API
  • 20. Adding a Widget (continued) Finally we create the widget by extending the WordPress Widget class: class Demo_Widget extends WP_Widget { public function __construct() { // widget actual processes parent::__construct( 'demo_widget', // Base ID 'Demo Widget', // Name array( 'description' => __( 'My Little Demo Widget', 'text_domain' ), ) // Arrgy ); } public function widget( $args, $instance ) { // outputs the content of the widget extract( $args ); $replaceWord = get_option('demo_myValue'); echo $before_widget; if ( ! empty( $replaceWord ) ) echo $before_title . 'My value' . $after_title . $replaceWord; echo $after_widget; } }
  • 21. Tip 1: Use a plugin prefix When creating a plugin, create a unique plugin prefix that can be used for all functions and variables. Since there are many plugins, it's important that your functions and variables don't conflict with other plugins.
  • 22. Tip 2: Never use PHP MySQL calls WordPress has a great database class called WPDB and makes it very each to plugin to the WordPress database. http://codex.wordpress.org/Class_Reference/wpdb For simple name/value pairs you can use WordPress options http://codex.wordpress.org/Function_Reference/add_option
  • 23. Tip 3: Queuing Scripts and Styles There are many cases where you may want to include a javascript or style sheet with your plugin. WordPress has this functionality built in. By default WordPress has many scripts included, such as jQuery. http://codex.wordpress.org/Function_Reference/wp_enqueue_script
  • 24. Tip 4: Use WordPress Admin Styles The WordPress admin has a style sheet that should be used when creating admin menus. The goal is to make your plugin fit seamless with WordPress. http://codex.wordpress.org/User:TECannon/UI_Pattern_and_Style_Guide
  • 25. Tip 5: Prepare your SQL Statements WordPress Database class has a function called prepare(). Use this function to properly prepare your SQL statements. http://codex.wordpress. org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
  • 26. There's a Plugin for That WordCamp Detroit 2010 http://bit.ly/wcdetplugins
  • 27. Thank you! Anthony Montalbano @italianst4 anthony@ambrdetroit.com