SlideShare a Scribd company logo
1 of 30
Download to read offline
Creating Your First
WordPress Plugin




                Brad Williams
                   @williamsba
Who Am I?

Brad Williams
Co-Founder of WebDevStudios.com
Organizer Philly WordPress Meetup
   & WordCamp Philly
Co-Author of Professional WordPress
   (http://bit.ly/pro-wp)
& Professional WordPress Plugin Development
   (http://amzn.to/plugindevbook)




           Slides available at: http://www.slideshare.net/williamsba
Topics
   What is a Plugin?
   Types of Plugins
   Sanity Practices and Plugin Foundation
   Determining Paths in a Plugin
   Activation, Deactivation, and Uninstall Methods
   Shortcodes, Menus, and Settings Overview
   Hooks: Actions and Filters Explained
   Resources for Plugin Developers
What is a Plugin?


 A plugin in WordPress is a PHP script
    that extends, or alters, the core
      functionality of WordPress.

Quite simply plugins are files installed in
 WordPress to add a feature, or set of
        features, to WordPress.
What is a Plugin?




http://wordpress.org/extend/plugins/
Types and Statuses of Plugins

   Active – Plugin is active and running in WordPress
   Inactive – Plugin is installed but not active. No code from the plugin is
    executed
   Must-Use – All plugins installed in wp-content/mu-plugins. All plugins
    are loaded automatically. Only way to deactivate is to remove.
   Drop-ins – Core functionality of WordPress can be replaced by Drop-in
    plugins.
       advanced-cache-php – Advanced caching plugin
       db.php – Custom database class
       maintenance.php – Custom maintenance message
       sunrise.php – Domain mapping
       And more…
Plugin Header Requirements
 <?php
 /*
 Plugin Name: WordCamp Philly 2011
 Plugin URI: http://webdevstudios.com/support/wordpress-plugins/
 Description: Plugin for WordCamp Philly
 Version: 1.0
 Author: Brad Williams
 Author URI: http://webdevstudios.com
 */
 ?>




Plugin is now available to be
activated in WordPress!
Sanity Practices
Prefix Everything!
update_option() //BAD FUNCTION NAME 

bw_wcphilly_update_option() //GOOD FUNCTION NAME! 

$settings //BAD VARIABLE NAME 

$bw_wcphilly_settings //GOOD VARIABLE NAME! 



Organized Folder Structure
   • /unique-plugin-name
        • unique-plugin-name.php          Keeping your files organized
        • uninstall.php                   using a clean folder structure
        • /js                             can make it much easier to
        • /css                            track the flow of your plugin over
        • /includes                       time.
        • /images
Determining Paths
Local Paths
<?php
//display local path to my plugin directory
echo plugin_dir_path( __FILE__ );
?>

Would display: /public_html/wp-content/plugins/my-new-plugin/

<?php
//display local path to my includes/functions.php file
echo plugin_dir_path( __FILE__ ) .‟includes/functions.php‟;
?>

Would display: /public_html/wp-content/plugins/my-new-plugin/includes/functions.php



         __FILE__ is a “magical” PHP
         constant containing the full
         path and filename of the file
Determining Paths
URL Paths
<?php
//display the URL to images/icon.png
echo plugins_url( 'images/icon.png', __FILE__ );
?>

Would display: http://example.com/wp-content/plugins/my-new-plugin/images/icon.png



  Advantages of plugins_url()
  • Supports the mu-plugins directory
  • Auto detects SSL, so if enabled the URL would return https
  • Uses the WP_PLUGIN_URL constant, meaning it can detect the correct path even if
  /wp-content has been moved
  • Supports Multisite using the WPMU_PLUGIN_URL constant
Important Techniques
Plugin Activation Function
<?php
register_activation_hook( __FILE__, 'bw_wcphilly_install' );

function bw_wcphilly_install() {
  if ( version_compare( get_bloginfo( 'version' ), „4.0', '<' ) ) {
      deactivate_plugins( plugin_basename( __FILE__ ) ); // Deactivate our plugin
      wp_die( 'This plugin requires WordPress version 4.0 or higher.' );
  }
}
?>



 register_activation_hook( $file, $function )

 Parameters:
     • $file (string) (required) – Path to the primary plugin file
     • $function (string) (required) – Function to be executed when plugin is
     activated


      http://codex.wordpress.org/Function_Reference/register_activation_hook
Important Techniques
Plugin Deactivation Function
<?php
register_deactivation_hook( __FILE__, 'bw_wcphilly_deactivate' );

function bw_wcphilly_deactivate() {
  //do stuff
}?>

 register_deactivation_hook( $file, $function )

 Parameters:
     • $file (string) (required) – Path to the primary plugin file
     • $function (string) (required) – Function to be executed when plugin is
     deactivated


               REMEMBER: Deactivating is NOT uninstalling. Always
               assume your users will reactivate at a later date.


    http://codex.wordpress.org/Function_Reference/register_deactivation_hook
Important Techniques
Plugin Uninstall
   1. Create a uninstall.php file in the root directory of your plugin
   2. Add all uninstall code to this file
<?php
if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
    exit ();

// Delete option from options table
delete_option( „bw_wcphilly_options' );
?>



                     If the WP_UNINSTALL_PLUGIN constant is not
                     defined we know WordPress did not call this
                     file. This is a security measure in WordPress


                 Uninstall.php is the recommended uninstall method, but
                 there is another method called register_uninstall_hook()

   http://jacobsantos.com/2008/general/wordpress-27-plugin-uninstall-methods/
What is a Hook?


      Hooks enable plugin developers to “hook”
      into WordPress and change how it works
      without modifying the core code


There are two kinds of hooks: Actions and Filters




http://jacobsantos.com/2008/general/wordpress-27-plugin-uninstall-methods/
Hooks
 Action Hooks
Enables you to execute a function at specific points in the WordPress loading process

<?php
add_action( 'user_register', 'bw_wcphilly_welcome_email' );

function bw_wcphilly_welcome_email( $user_id ) {

  $user_info = get_userdata( $user_id );

  //populate email values
  $email_to = is_email( $user_info->user_email );
  $email_subject = 'Welcome!';
  $email_msg = 'Thank you for registering on my website!';

  //send welcome email
  wp_mail( $email_to, $email_subject, $email_msg );

}
?>



                http://codex.wordpress.org/Plugin_API/Action_Reference
Hooks
Common Action Hooks
 • plugins_loaded – Earliest hook in the WP loading process, after all plugins have
 been loaded
 • init – Fire after most of WP is set up so all information is available
  admin_menu – Runs when the basic admin menu structure is in place
 • template_redirect – Executed just before the theme template is chosen
 • wp_head – Executed on the front end of WordPress between the <head> tags
 • wp_footer – Runs in the footer of your theme
 • admin_head – Executed on the admin side of WordPress between the <head>
 tags
 • admin_footer – Runs in the footer of the admin side of WordPress
 • user_register – Executes when a new user account is created
 • save_post – Runs when a post or page is created or updated




          http://codex.wordpress.org/Plugin_API/Action_Reference
Hooks
Filter Hooks
     Enables you to manipulate the output of code and content in WordPress

<?php
add_filter( 'the_content', 'bw_wcphilly_filter_content' );

function bw_wcphilly_filter_content( $text ) {

  $text = str_replace( 'Drupal', 'WordPress', $text );

  return $text;

}
?>

           Change every instance of Drupal to WordPress in your content >:)

                      REMEMBER: Using a filter does NOT change the
                      content in the database, it simply alters it prior
                      to displaying

                  http://codex.wordpress.org/Plugin_API/Filter_Reference
Hooks
Filter Hooks
<?php
add_filter ( 'the_content', „bw_wcphilly_insertFootNote' );

function bw_wcphilly_insertFootNote( $content ) {

    if( !is_feed() && !is_home() ) {
           $content .= "<div class='subscribe'>";
           $content .= "<h4>Enjoyed this article?</h4>";
           $content .= "<p>Subscribe to my <a href='http://feeds2.feedburner.com/strangework'>RSS
feed</a>!</p>";
           $content .= "</div>";
    }

     return $content;
}
?>




                 http://codex.wordpress.org/Plugin_API/Filter_Reference
Hooks
Common Filter Hooks
 • the_content – Filter applied to the content of the post or page
 • the_title – Applied to the post title
 • body_class – Applied to the <body> tag class parameter
 • default_content – Applied to the content on a new post or page
 • comment_text – Applied to the comment text of a comment




           http://codex.wordpress.org/Plugin_API/Filter_Reference
Hooks
Number of Hooks in WordPress by Version




             http://adambrown.info/p/wp_hooks
Plugin Foundation




It‟s important to start with a solid foundation
Plugin Foundation
Example Plugin Foundation
<?php
/*
Plugin Name: WordCamp Philly
Plugin URI: http://webdevstudios.com/support/wordpress-plugins/
Description: Plugin for WordCamp Philly
Version: 1.0
Author: Brad Williams
Author URI: http://webdevstudios.com
License: GPLv2
*/

// DEFINE CONSTANTS
define( „WCPHILLY_VERSION', '1.0' );
define( 'WCPHILLY_TEXTDOMAIN', „wcphilly_plugin' );
define( 'WCPHILLY_BASENAME', plugin_basename(__FILE__) );
define( 'WCPHILLY_DIR', plugin_dir_path( __FILE__ ) );
define( 'WCPHILLY_URL', plugins_url( 'my-new-plugin/' ) );

require_once( WCPHILLY_DIR . 'includes/core.php' );
?>
Shortcode Example
Example
<?php
// Register a new shortcode: [book]
add_shortcode( 'book', 'bw_wcphilly_book' );

// The callback function that will replace [book]
function bw_wcphilly_book() {
             return '<a href="http://amzn.to/plugindevbook">Professional WordPress Plugin
Development</a>';
}
?>
Menu Example
        Example Custom Menu
<?php
add_action( 'admin_menu', 'bw_wcphilly_create_menu' );

function bw_wcphilly_create_menu() {

         //create custom top-level menu
         add_menu_page('WC Philly Settings Page', 'WC Philly',
                    'manage_options', 'wcphilly-plugin', 'bw_wcphilly_settings_page' );

         //create submenu items
         add_submenu_page( 'wcphilly-plugin', 'About My Plugin', 'About',
                    'manage_options', 'wcphilly-plugin_about', 'bw_wcphilly_about_page' );
         add_submenu_page( 'wcphilly-plugin', 'Help with My Plugin', 'Help',
                    'manage_options', 'wcphilly-plugin_help', 'bw_wcphilly_help_page' );
         add_submenu_page( 'wcphilly-plugin', 'Uinstall My Plugin', 'Uninstall',
                    'manage_options', 'nyc-meetup-plugin_uninstall', 'bw_wcphilly_uninstall_page' );

}
?>
Menu Example
add_menu_page()
Parameters:
$page_title – The title tag text
$menu_title – The menu name
$capability - Capability required to view menu
$menu_slug – Unique slug to reference menu by
$function – Function that displays the pages content
$icon_url – URL for a custom menu icon
$position – Position the menu should appear

add_submenu_page()
Parameters:
$parent_slug – Slug name for the parent menu
$page_title – The title tag text
$menu_title – The submenu name
$capability – Capability required to view submenu
$menu_slug – Unique slug to reference menu by
$function – Function that displays the pages content
Menu Example
Add Submenu to Custom Post Type
<?php
add_action( 'admin_menu', 'bw_wcphilly_create_menu' );

function bw_wcphilly_create_menu() {

//create submenu items
add_submenu_page( 'edit.php?post_type=movies', 'About My Plugin', 'About',
            'manage_options', „wcphilly-plugin_about', 'bw_wcphilly_about_page' );

}
?>




                        You can easily add a submenu to any Custom
                        Post Type menu by setting the $parent_slug to
                        edit.php?post_type=POSTTYPENAME
Menu Example
Add Submenu to Existing Menu
<?php
add_action( 'admin_menu', 'bw_wcphilly_create_menu' );

function bw_wcphilly_create_menu() {

//create Settings submenu page
   add_options_page('WC Philly Settings Page', 'WC Philly', 'manage_options',
'wcphilly-plugin', 'bw_wcphilly_settings_page' );

}

function bw_wcphilly_settings_page() {
  //settings page
}?>
Menu Example
add_options_page()
Parameters:
$page_title – The title tag text
$menu_title – The menu name
$capability - Capability required to view menu
$menu_slug – Unique slug to reference menu by
$function – Function that displays the pages content

       add_dashboard_page()
       add_posts_page()
       add_media_page()
       add_links_page()
       add_pages_page()
       add_comments_page()
       add_theme_page()
       add_plugins_page()
       add_users_page()
       add_management_page()
       add_options_page()
Plugin Developer Resources
   Official Resources
     ›   WordPress Core!
     ›   http://wordpress.org/extend/plugins/
     ›   http://codex.wordpress.org/Writing_a_Plugin
     ›   http://codex.wordpress.org/Data_Validation
     ›   http://wordpress.org/support/forum/hacks
     ›   http://lists.automattic.com/mailman/listinfo/wp-hackers
     ›   http://codex.wordpress.org/IRC ( #wordpress channel )
     ›   http://wpdevel.wordpress.com/
     ›   http://wordpress.org/extend/ideas/


   Developer Websites
     ›   http://wpengineer.com
     ›   http://phpxref.ftwr.co.uk/wordpress/
     ›   http://adambrown.info/p/wp_hooks
Contact

Brad Williams
brad@webdevstudios.com

Blog: strangework.com
Twitter: @williamsba
IRC: WDS-Brad

       http://www.slideshare.net/williamsba

More Related Content

What's hot

Using composer with WordPress
Using composer with WordPressUsing composer with WordPress
Using composer with WordPressMicah Wood
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern ApproachAlessandro Fiore
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsYameen Khan
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.SWAAM Tech
 
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7Max Andersen
 
15.exemplu complet eloquent view add-edit-delete-search
15.exemplu complet eloquent view add-edit-delete-search15.exemplu complet eloquent view add-edit-delete-search
15.exemplu complet eloquent view add-edit-delete-searchRazvan Raducanu, PhD
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
So, you want to be a plugin developer?
So, you want to be a plugin developer?So, you want to be a plugin developer?
So, you want to be a plugin developer?ylefebvre
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingDougal Campbell
 
Learn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLearn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLarry Cai
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Edureka!
 

What's hot (20)

Using composer with WordPress
Using composer with WordPressUsing composer with WordPress
Using composer with WordPress
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress Experts
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.
 
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7
Red Hat and Oracle: Delivering on the Promise of Interoperability in Java EE 7
 
Intro to Laravel 4
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4
 
ADF in action 1.2
ADF in action 1.2ADF in action 1.2
ADF in action 1.2
 
wp-cli
wp-cliwp-cli
wp-cli
 
15.exemplu complet eloquent view add-edit-delete-search
15.exemplu complet eloquent view add-edit-delete-search15.exemplu complet eloquent view add-edit-delete-search
15.exemplu complet eloquent view add-edit-delete-search
 
The wp config.php
The wp config.phpThe wp config.php
The wp config.php
 
Installing AtoM with Ansible
Installing AtoM with AnsibleInstalling AtoM with Ansible
Installing AtoM with Ansible
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
So, you want to be a plugin developer?
So, you want to be a plugin developer?So, you want to be a plugin developer?
So, you want to be a plugin developer?
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
 
Learn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLearn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutes
 
Flask
FlaskFlask
Flask
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
 

Viewers also liked

How to create your own WordPress plugin
How to create your own WordPress pluginHow to create your own WordPress plugin
How to create your own WordPress pluginJohn Tighe
 
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
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin developmentarryaas
 
Wordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short TutorialWordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short TutorialChristos Zigkolis
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comKathy Gill
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011photomatt
 

Viewers also liked (6)

How to create your own WordPress plugin
How to create your own WordPress pluginHow to create your own WordPress plugin
How to create your own WordPress plugin
 
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
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin development
 
Wordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short TutorialWordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short Tutorial
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.com
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
 

Similar to Creating Your First WordPress Plugin

Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practicesdanpastori
 
Introduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentIntroduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentBruce L Chamoff
 
Beginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentBeginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentAizat Faiz
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPressEdward Caissie
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For BegineersM A Hossain Tonu
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress PluginAndy Stratton
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017ylefebvre
 
<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into WordpressMatt Harris
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Pluginsrandyhoyt
 
Now That's What I Call WordPress Security 2010
Now That's What I Call WordPress Security 2010Now That's What I Call WordPress Security 2010
Now That's What I Call WordPress Security 2010Brad Williams
 
Intro to Plugin Development, Miami WordCamp, 2015
Intro to Plugin Development, Miami WordCamp, 2015Intro to Plugin Development, Miami WordCamp, 2015
Intro to Plugin Development, Miami WordCamp, 2015topher1kenobe
 
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
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
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
 
Complete Wordpress Security By CHETAN SONI - Cyber Security Expert
Complete Wordpress Security By CHETAN SONI - Cyber Security ExpertComplete Wordpress Security By CHETAN SONI - Cyber Security Expert
Complete Wordpress Security By CHETAN SONI - Cyber Security ExpertChetan Soni
 
Developing WordPress Plugins
Developing WordPress PluginsDeveloping WordPress Plugins
Developing WordPress Pluginsrebelpixel
 

Similar to Creating Your First WordPress Plugin (20)

Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practices
 
Introduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentIntroduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin Development
 
Beginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentBeginning WordPress Plugin Development
Beginning WordPress Plugin Development
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPress
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
WordPress Security
WordPress SecurityWordPress Security
WordPress Security
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For Begineers
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
 
<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Now That's What I Call WordPress Security 2010
Now That's What I Call WordPress Security 2010Now That's What I Call WordPress Security 2010
Now That's What I Call WordPress Security 2010
 
Intro to Plugin Development, Miami WordCamp, 2015
Intro to Plugin Development, Miami WordCamp, 2015Intro to Plugin Development, Miami WordCamp, 2015
Intro to Plugin Development, Miami WordCamp, 2015
 
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
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
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
 
Complete Wordpress Security By CHETAN SONI - Cyber Security Expert
Complete Wordpress Security By CHETAN SONI - Cyber Security ExpertComplete Wordpress Security By CHETAN SONI - Cyber Security Expert
Complete Wordpress Security By CHETAN SONI - Cyber Security Expert
 
Developing WordPress Plugins
Developing WordPress PluginsDeveloping WordPress Plugins
Developing WordPress Plugins
 

More from Brad Williams

From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015
From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015
From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015Brad Williams
 
Hiring Employee Number One: From Freelancer to Agency
Hiring Employee Number One: From Freelancer to AgencyHiring Employee Number One: From Freelancer to Agency
Hiring Employee Number One: From Freelancer to AgencyBrad Williams
 
Writing Secure WordPress Code WordCamp NYC 2014
Writing Secure WordPress Code WordCamp NYC 2014Writing Secure WordPress Code WordCamp NYC 2014
Writing Secure WordPress Code WordCamp NYC 2014Brad Williams
 
How to Make a Native Mobile App with WordPress
How to Make a Native Mobile App with WordPressHow to Make a Native Mobile App with WordPress
How to Make a Native Mobile App with WordPressBrad Williams
 
Writing Secure WordPress Code
Writing Secure WordPress CodeWriting Secure WordPress Code
Writing Secure WordPress CodeBrad Williams
 
WordPress Security WordCamp OC 2013
WordPress Security WordCamp OC 2013WordPress Security WordCamp OC 2013
WordPress Security WordCamp OC 2013Brad Williams
 
Using WordPress as an Application Framework
Using WordPress as an Application FrameworkUsing WordPress as an Application Framework
Using WordPress as an Application FrameworkBrad Williams
 
Top Ten WordPress Security Tips for 2012
Top Ten WordPress Security Tips for 2012Top Ten WordPress Security Tips for 2012
Top Ten WordPress Security Tips for 2012Brad Williams
 
WordPress Security from WordCamp NYC 2012
WordPress Security from WordCamp NYC 2012WordPress Security from WordCamp NYC 2012
WordPress Security from WordCamp NYC 2012Brad Williams
 
WordPress for Beginners
WordPress for BeginnersWordPress for Beginners
WordPress for BeginnersBrad Williams
 
Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies
Surviving the Zombie Apocalypse using Custom Post Types and TaxonomiesSurviving the Zombie Apocalypse using Custom Post Types and Taxonomies
Surviving the Zombie Apocalypse using Custom Post Types and TaxonomiesBrad Williams
 
Spooky WordPress: Disturbingly Brilliant Uses of WP
Spooky WordPress: Disturbingly Brilliant Uses of WPSpooky WordPress: Disturbingly Brilliant Uses of WP
Spooky WordPress: Disturbingly Brilliant Uses of WPBrad Williams
 
WordCamp Mid-Atlantic WordPress Security
WordCamp Mid-Atlantic WordPress SecurityWordCamp Mid-Atlantic WordPress Security
WordCamp Mid-Atlantic WordPress SecurityBrad Williams
 
Custom Post Types and Taxonomies in WordPress
Custom Post Types and Taxonomies in WordPressCustom Post Types and Taxonomies in WordPress
Custom Post Types and Taxonomies in WordPressBrad Williams
 
Top 20 WordPress Plugins You've Never Heard Of
Top 20 WordPress Plugins You've Never Heard OfTop 20 WordPress Plugins You've Never Heard Of
Top 20 WordPress Plugins You've Never Heard OfBrad Williams
 
WordPress Security - WordCamp Boston 2010
WordPress Security - WordCamp Boston 2010WordPress Security - WordCamp Boston 2010
WordPress Security - WordCamp Boston 2010Brad Williams
 
WordPress Security - WordCamp NYC 2009
WordPress Security - WordCamp NYC 2009WordPress Security - WordCamp NYC 2009
WordPress Security - WordCamp NYC 2009Brad Williams
 
Website Design Dos and Don’ts for a Successful Online Presence
Website Design Dos and Don’ts  for a Successful Online PresenceWebsite Design Dos and Don’ts  for a Successful Online Presence
Website Design Dos and Don’ts for a Successful Online PresenceBrad Williams
 
WordPress Security Updated - NYC Meetup 2009
WordPress Security Updated - NYC Meetup 2009WordPress Security Updated - NYC Meetup 2009
WordPress Security Updated - NYC Meetup 2009Brad Williams
 

More from Brad Williams (20)

From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015
From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015
From Freelance to Agency: Hiring Employee Number One - WordCamp London 2015
 
Hiring Employee Number One: From Freelancer to Agency
Hiring Employee Number One: From Freelancer to AgencyHiring Employee Number One: From Freelancer to Agency
Hiring Employee Number One: From Freelancer to Agency
 
Writing Secure WordPress Code WordCamp NYC 2014
Writing Secure WordPress Code WordCamp NYC 2014Writing Secure WordPress Code WordCamp NYC 2014
Writing Secure WordPress Code WordCamp NYC 2014
 
How to Make a Native Mobile App with WordPress
How to Make a Native Mobile App with WordPressHow to Make a Native Mobile App with WordPress
How to Make a Native Mobile App with WordPress
 
Writing Secure WordPress Code
Writing Secure WordPress CodeWriting Secure WordPress Code
Writing Secure WordPress Code
 
WordPress Security WordCamp OC 2013
WordPress Security WordCamp OC 2013WordPress Security WordCamp OC 2013
WordPress Security WordCamp OC 2013
 
Using WordPress as an Application Framework
Using WordPress as an Application FrameworkUsing WordPress as an Application Framework
Using WordPress as an Application Framework
 
Top Ten WordPress Security Tips for 2012
Top Ten WordPress Security Tips for 2012Top Ten WordPress Security Tips for 2012
Top Ten WordPress Security Tips for 2012
 
WordPress Security from WordCamp NYC 2012
WordPress Security from WordCamp NYC 2012WordPress Security from WordCamp NYC 2012
WordPress Security from WordCamp NYC 2012
 
WordPress Multisite
WordPress MultisiteWordPress Multisite
WordPress Multisite
 
WordPress for Beginners
WordPress for BeginnersWordPress for Beginners
WordPress for Beginners
 
Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies
Surviving the Zombie Apocalypse using Custom Post Types and TaxonomiesSurviving the Zombie Apocalypse using Custom Post Types and Taxonomies
Surviving the Zombie Apocalypse using Custom Post Types and Taxonomies
 
Spooky WordPress: Disturbingly Brilliant Uses of WP
Spooky WordPress: Disturbingly Brilliant Uses of WPSpooky WordPress: Disturbingly Brilliant Uses of WP
Spooky WordPress: Disturbingly Brilliant Uses of WP
 
WordCamp Mid-Atlantic WordPress Security
WordCamp Mid-Atlantic WordPress SecurityWordCamp Mid-Atlantic WordPress Security
WordCamp Mid-Atlantic WordPress Security
 
Custom Post Types and Taxonomies in WordPress
Custom Post Types and Taxonomies in WordPressCustom Post Types and Taxonomies in WordPress
Custom Post Types and Taxonomies in WordPress
 
Top 20 WordPress Plugins You've Never Heard Of
Top 20 WordPress Plugins You've Never Heard OfTop 20 WordPress Plugins You've Never Heard Of
Top 20 WordPress Plugins You've Never Heard Of
 
WordPress Security - WordCamp Boston 2010
WordPress Security - WordCamp Boston 2010WordPress Security - WordCamp Boston 2010
WordPress Security - WordCamp Boston 2010
 
WordPress Security - WordCamp NYC 2009
WordPress Security - WordCamp NYC 2009WordPress Security - WordCamp NYC 2009
WordPress Security - WordCamp NYC 2009
 
Website Design Dos and Don’ts for a Successful Online Presence
Website Design Dos and Don’ts  for a Successful Online PresenceWebsite Design Dos and Don’ts  for a Successful Online Presence
Website Design Dos and Don’ts for a Successful Online Presence
 
WordPress Security Updated - NYC Meetup 2009
WordPress Security Updated - NYC Meetup 2009WordPress Security Updated - NYC Meetup 2009
WordPress Security Updated - NYC Meetup 2009
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Creating Your First WordPress Plugin

  • 1. Creating Your First WordPress Plugin Brad Williams @williamsba
  • 2. Who Am I? Brad Williams Co-Founder of WebDevStudios.com Organizer Philly WordPress Meetup & WordCamp Philly Co-Author of Professional WordPress (http://bit.ly/pro-wp) & Professional WordPress Plugin Development (http://amzn.to/plugindevbook) Slides available at: http://www.slideshare.net/williamsba
  • 3. Topics  What is a Plugin?  Types of Plugins  Sanity Practices and Plugin Foundation  Determining Paths in a Plugin  Activation, Deactivation, and Uninstall Methods  Shortcodes, Menus, and Settings Overview  Hooks: Actions and Filters Explained  Resources for Plugin Developers
  • 4. What is a Plugin? A plugin in WordPress is a PHP script that extends, or alters, the core functionality of WordPress. Quite simply plugins are files installed in WordPress to add a feature, or set of features, to WordPress.
  • 5. What is a Plugin? http://wordpress.org/extend/plugins/
  • 6. Types and Statuses of Plugins  Active – Plugin is active and running in WordPress  Inactive – Plugin is installed but not active. No code from the plugin is executed  Must-Use – All plugins installed in wp-content/mu-plugins. All plugins are loaded automatically. Only way to deactivate is to remove.  Drop-ins – Core functionality of WordPress can be replaced by Drop-in plugins.  advanced-cache-php – Advanced caching plugin  db.php – Custom database class  maintenance.php – Custom maintenance message  sunrise.php – Domain mapping  And more…
  • 7. Plugin Header Requirements <?php /* Plugin Name: WordCamp Philly 2011 Plugin URI: http://webdevstudios.com/support/wordpress-plugins/ Description: Plugin for WordCamp Philly Version: 1.0 Author: Brad Williams Author URI: http://webdevstudios.com */ ?> Plugin is now available to be activated in WordPress!
  • 8. Sanity Practices Prefix Everything! update_option() //BAD FUNCTION NAME  bw_wcphilly_update_option() //GOOD FUNCTION NAME!  $settings //BAD VARIABLE NAME  $bw_wcphilly_settings //GOOD VARIABLE NAME!  Organized Folder Structure • /unique-plugin-name • unique-plugin-name.php Keeping your files organized • uninstall.php using a clean folder structure • /js can make it much easier to • /css track the flow of your plugin over • /includes time. • /images
  • 9. Determining Paths Local Paths <?php //display local path to my plugin directory echo plugin_dir_path( __FILE__ ); ?> Would display: /public_html/wp-content/plugins/my-new-plugin/ <?php //display local path to my includes/functions.php file echo plugin_dir_path( __FILE__ ) .‟includes/functions.php‟; ?> Would display: /public_html/wp-content/plugins/my-new-plugin/includes/functions.php __FILE__ is a “magical” PHP constant containing the full path and filename of the file
  • 10. Determining Paths URL Paths <?php //display the URL to images/icon.png echo plugins_url( 'images/icon.png', __FILE__ ); ?> Would display: http://example.com/wp-content/plugins/my-new-plugin/images/icon.png Advantages of plugins_url() • Supports the mu-plugins directory • Auto detects SSL, so if enabled the URL would return https • Uses the WP_PLUGIN_URL constant, meaning it can detect the correct path even if /wp-content has been moved • Supports Multisite using the WPMU_PLUGIN_URL constant
  • 11. Important Techniques Plugin Activation Function <?php register_activation_hook( __FILE__, 'bw_wcphilly_install' ); function bw_wcphilly_install() { if ( version_compare( get_bloginfo( 'version' ), „4.0', '<' ) ) { deactivate_plugins( plugin_basename( __FILE__ ) ); // Deactivate our plugin wp_die( 'This plugin requires WordPress version 4.0 or higher.' ); } } ?> register_activation_hook( $file, $function ) Parameters: • $file (string) (required) – Path to the primary plugin file • $function (string) (required) – Function to be executed when plugin is activated http://codex.wordpress.org/Function_Reference/register_activation_hook
  • 12. Important Techniques Plugin Deactivation Function <?php register_deactivation_hook( __FILE__, 'bw_wcphilly_deactivate' ); function bw_wcphilly_deactivate() { //do stuff }?> register_deactivation_hook( $file, $function ) Parameters: • $file (string) (required) – Path to the primary plugin file • $function (string) (required) – Function to be executed when plugin is deactivated REMEMBER: Deactivating is NOT uninstalling. Always assume your users will reactivate at a later date. http://codex.wordpress.org/Function_Reference/register_deactivation_hook
  • 13. Important Techniques Plugin Uninstall 1. Create a uninstall.php file in the root directory of your plugin 2. Add all uninstall code to this file <?php if( !defined( 'WP_UNINSTALL_PLUGIN' ) ) exit (); // Delete option from options table delete_option( „bw_wcphilly_options' ); ?> If the WP_UNINSTALL_PLUGIN constant is not defined we know WordPress did not call this file. This is a security measure in WordPress Uninstall.php is the recommended uninstall method, but there is another method called register_uninstall_hook() http://jacobsantos.com/2008/general/wordpress-27-plugin-uninstall-methods/
  • 14. What is a Hook? Hooks enable plugin developers to “hook” into WordPress and change how it works without modifying the core code There are two kinds of hooks: Actions and Filters http://jacobsantos.com/2008/general/wordpress-27-plugin-uninstall-methods/
  • 15. Hooks Action Hooks Enables you to execute a function at specific points in the WordPress loading process <?php add_action( 'user_register', 'bw_wcphilly_welcome_email' ); function bw_wcphilly_welcome_email( $user_id ) { $user_info = get_userdata( $user_id ); //populate email values $email_to = is_email( $user_info->user_email ); $email_subject = 'Welcome!'; $email_msg = 'Thank you for registering on my website!'; //send welcome email wp_mail( $email_to, $email_subject, $email_msg ); } ?> http://codex.wordpress.org/Plugin_API/Action_Reference
  • 16. Hooks Common Action Hooks • plugins_loaded – Earliest hook in the WP loading process, after all plugins have been loaded • init – Fire after most of WP is set up so all information is available admin_menu – Runs when the basic admin menu structure is in place • template_redirect – Executed just before the theme template is chosen • wp_head – Executed on the front end of WordPress between the <head> tags • wp_footer – Runs in the footer of your theme • admin_head – Executed on the admin side of WordPress between the <head> tags • admin_footer – Runs in the footer of the admin side of WordPress • user_register – Executes when a new user account is created • save_post – Runs when a post or page is created or updated http://codex.wordpress.org/Plugin_API/Action_Reference
  • 17. Hooks Filter Hooks Enables you to manipulate the output of code and content in WordPress <?php add_filter( 'the_content', 'bw_wcphilly_filter_content' ); function bw_wcphilly_filter_content( $text ) { $text = str_replace( 'Drupal', 'WordPress', $text ); return $text; } ?> Change every instance of Drupal to WordPress in your content >:) REMEMBER: Using a filter does NOT change the content in the database, it simply alters it prior to displaying http://codex.wordpress.org/Plugin_API/Filter_Reference
  • 18. Hooks Filter Hooks <?php add_filter ( 'the_content', „bw_wcphilly_insertFootNote' ); function bw_wcphilly_insertFootNote( $content ) { if( !is_feed() && !is_home() ) { $content .= "<div class='subscribe'>"; $content .= "<h4>Enjoyed this article?</h4>"; $content .= "<p>Subscribe to my <a href='http://feeds2.feedburner.com/strangework'>RSS feed</a>!</p>"; $content .= "</div>"; } return $content; } ?> http://codex.wordpress.org/Plugin_API/Filter_Reference
  • 19. Hooks Common Filter Hooks • the_content – Filter applied to the content of the post or page • the_title – Applied to the post title • body_class – Applied to the <body> tag class parameter • default_content – Applied to the content on a new post or page • comment_text – Applied to the comment text of a comment http://codex.wordpress.org/Plugin_API/Filter_Reference
  • 20. Hooks Number of Hooks in WordPress by Version http://adambrown.info/p/wp_hooks
  • 21. Plugin Foundation It‟s important to start with a solid foundation
  • 22. Plugin Foundation Example Plugin Foundation <?php /* Plugin Name: WordCamp Philly Plugin URI: http://webdevstudios.com/support/wordpress-plugins/ Description: Plugin for WordCamp Philly Version: 1.0 Author: Brad Williams Author URI: http://webdevstudios.com License: GPLv2 */ // DEFINE CONSTANTS define( „WCPHILLY_VERSION', '1.0' ); define( 'WCPHILLY_TEXTDOMAIN', „wcphilly_plugin' ); define( 'WCPHILLY_BASENAME', plugin_basename(__FILE__) ); define( 'WCPHILLY_DIR', plugin_dir_path( __FILE__ ) ); define( 'WCPHILLY_URL', plugins_url( 'my-new-plugin/' ) ); require_once( WCPHILLY_DIR . 'includes/core.php' ); ?>
  • 23. Shortcode Example Example <?php // Register a new shortcode: [book] add_shortcode( 'book', 'bw_wcphilly_book' ); // The callback function that will replace [book] function bw_wcphilly_book() { return '<a href="http://amzn.to/plugindevbook">Professional WordPress Plugin Development</a>'; } ?>
  • 24. Menu Example Example Custom Menu <?php add_action( 'admin_menu', 'bw_wcphilly_create_menu' ); function bw_wcphilly_create_menu() { //create custom top-level menu add_menu_page('WC Philly Settings Page', 'WC Philly', 'manage_options', 'wcphilly-plugin', 'bw_wcphilly_settings_page' ); //create submenu items add_submenu_page( 'wcphilly-plugin', 'About My Plugin', 'About', 'manage_options', 'wcphilly-plugin_about', 'bw_wcphilly_about_page' ); add_submenu_page( 'wcphilly-plugin', 'Help with My Plugin', 'Help', 'manage_options', 'wcphilly-plugin_help', 'bw_wcphilly_help_page' ); add_submenu_page( 'wcphilly-plugin', 'Uinstall My Plugin', 'Uninstall', 'manage_options', 'nyc-meetup-plugin_uninstall', 'bw_wcphilly_uninstall_page' ); } ?>
  • 25. Menu Example add_menu_page() Parameters: $page_title – The title tag text $menu_title – The menu name $capability - Capability required to view menu $menu_slug – Unique slug to reference menu by $function – Function that displays the pages content $icon_url – URL for a custom menu icon $position – Position the menu should appear add_submenu_page() Parameters: $parent_slug – Slug name for the parent menu $page_title – The title tag text $menu_title – The submenu name $capability – Capability required to view submenu $menu_slug – Unique slug to reference menu by $function – Function that displays the pages content
  • 26. Menu Example Add Submenu to Custom Post Type <?php add_action( 'admin_menu', 'bw_wcphilly_create_menu' ); function bw_wcphilly_create_menu() { //create submenu items add_submenu_page( 'edit.php?post_type=movies', 'About My Plugin', 'About', 'manage_options', „wcphilly-plugin_about', 'bw_wcphilly_about_page' ); } ?> You can easily add a submenu to any Custom Post Type menu by setting the $parent_slug to edit.php?post_type=POSTTYPENAME
  • 27. Menu Example Add Submenu to Existing Menu <?php add_action( 'admin_menu', 'bw_wcphilly_create_menu' ); function bw_wcphilly_create_menu() { //create Settings submenu page add_options_page('WC Philly Settings Page', 'WC Philly', 'manage_options', 'wcphilly-plugin', 'bw_wcphilly_settings_page' ); } function bw_wcphilly_settings_page() { //settings page }?>
  • 28. Menu Example add_options_page() Parameters: $page_title – The title tag text $menu_title – The menu name $capability - Capability required to view menu $menu_slug – Unique slug to reference menu by $function – Function that displays the pages content add_dashboard_page() add_posts_page() add_media_page() add_links_page() add_pages_page() add_comments_page() add_theme_page() add_plugins_page() add_users_page() add_management_page() add_options_page()
  • 29. Plugin Developer Resources  Official Resources › WordPress Core! › http://wordpress.org/extend/plugins/ › http://codex.wordpress.org/Writing_a_Plugin › http://codex.wordpress.org/Data_Validation › http://wordpress.org/support/forum/hacks › http://lists.automattic.com/mailman/listinfo/wp-hackers › http://codex.wordpress.org/IRC ( #wordpress channel ) › http://wpdevel.wordpress.com/ › http://wordpress.org/extend/ideas/  Developer Websites › http://wpengineer.com › http://phpxref.ftwr.co.uk/wordpress/ › http://adambrown.info/p/wp_hooks
  • 30. Contact Brad Williams brad@webdevstudios.com Blog: strangework.com Twitter: @williamsba IRC: WDS-Brad http://www.slideshare.net/williamsba