SlideShare a Scribd company logo
WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email_address]
What is a Plugin? WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. Instead of changing the core programming of WordPress, you can add functionality with WordPress Plugins. Here is a basic definition: WordPress Plugin: A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API).  Swiped from  http://codex.wordpress.org/Writing_a_Plugin
Why Write a Plugin? Solve a problem Extend existing functionality Save time Portability (changing themes, using on multiple sites) Make money (???)
Plugin Basics 1 or more files placed in the  wp-content/plugins  folder 1 file should have the header so WP recognizes it Can integrate into the admin as well your site pages and posts Can extend or even  remove   existing WP functionality (careful!)
Plugin Header One of your plugin files must have this header for WP to recognize it: <?php /* Plugin Name: Header and Footer Plugin URI: http://www.satollo.net/plugins/header-footer Description: Lets you to add code to the head and footer of pages. Author: Satollo Author URI: http://www.satollo.net */ ?>
Plugin License Most WordPress plugins use the GPL2 license also used by WordPress. To use it, include the following in your plugin file: <?php /*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as  published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */ ?>
Plugin Code May Include: Hooks to  Actions  or  Filters Hooks define how your plugin interacts with WordPress Functions Defined Constants New Admin pages & saved options
Actions hooks are triggered by events in WP such as  wp_head ,  wp_footer ,  admin_init ,  register_user ,  comment_post Syntax is: P=Priority (numeric, defaults to 10, lower the number the earlier it executes) N=Number of arguments the function can accept Example: Action Hooks add_action(‘hook-name’, ‘function-name’) add_action(‘hook-name’, ‘function-name’, P, N) add_action( 'admin_notices', 'hello_dolly' ); function hello_dolly() { $chosen = hello_dolly_get_lyric(); echo &quot;<p id='dolly'>$chosen</p>&quot;; }
Filter Hooks Filter hooks are used to modify text before adding it to the database or displaying it on screen – examples include  the_content ,  the_title ,  posts_where Syntax is: P=Priority (numeric, defaults to 10, lower the number the earlier it executes) N=Number of arguments the function can accept Example: add_filter(‘hook-name’, ‘function-name’); add_filter(‘hook-name’, ‘function-name’, P, N); add_filter( ‘the_title', ‘star_titles' );
Filter Functions A filter is altering text (or potentially altering text) Therefore, a filter function is receiving  text  as a parameter and must return  text  as a return value Example: add_filter( ‘the_title', ‘star_titles' ); function star_titles($title) { return ‘*** ‘ . $title . ‘ ***’; }
Removing WP Functionality Sometimes a plugin is designed to remove something WP is already doing Syntax is: Obviously, be very careful when doing this! remove_action(‘hook-name’, ‘function-name’); remove_filter(‘hook-name’, ‘function-name’);
Changes to WP Admin Add an Options page within Admin Save a new plugin option in WP Database: Update an option in WP Database: (even someone else’s): Retrieve an option from WP Database: add_options_page(‘page_title’,‘menu_title’,‘capability’,’menu-slug’,’function-name’); add_options_page(‘Footer Edit’,‘Footer Edit’,‘manage_options’,’footer-edit’, ’ footer-edit/footer.php’); add_option(‘option_name’,‘option_value’); add_option(‘footer_text’,‘Copyright 2011’); update_option(‘option_name’,‘option_value’); get_option(‘option_name’);
Saving Data For small bits of data to be saved, using the WP options table works well Data writes are time consuming. To save time writing and pulling data, can combine bits of data into an array. add_option(‘first_name’,‘Jane’); add_option(‘middle_name’,‘Ann’); add_option(‘last_name’,‘Smith’); $name_arr = array( “ first_name” => ‘Jane’, “ middle_name” => ‘Ann’, “ last_name” => ‘Smith’); add_option(‘full_name’, $name_arr ); Can be written as What if your plugin requires much more data  to be stored in the database???
Creating new database tables Define table name: global $wpdb;  $table_name = $wpdb->prefix . “contributor_names&quot;;  Check if table already exists: if($wpdb->get_var(&quot;SHOW TABLES LIKE '$table_name'&quot;) != $table_name)  Add table using dbDelta function: $sql = &quot;CREATE TABLE &quot; . $table_name . &quot; (   id mediumint(9) NOT NULL AUTO_INCREMENT,   first_name tinytext NOT NULL,   middle_name tinytext NOT NULL,   last_name tinytext NOT NULL,   url VARCHAR(200) NOT NULL,   UNIQUE KEY id (id) );&quot;; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql);
Other Considerations Ensure your plugin has a  unique  folder name (or  unique  file names if not in a folder) Use  unique  function names. Having a uniform prefix  for all your functions can help with this. Very important! WP Version Checking within code, or check if specific WP function exists before calling Internationalization Licensing Documentation, Comments, or ReadMe.txt if ( function_exists ( '   has_term') )
Internationalization Goal is to mark strings which can be translated (even if no current translation exists) Good idea if you plan on distributing your plugin Translate String syntax: Function name begins with 2 underscores back to back The ‘text_domain’ is a unique identifier, which makes sure WP can distinguish between all loaded translations. Using the basename of your plugin is always a good choice. _   _(‘string_to_translate’); _   _e(‘string_to_translate’); //Echoed to browser _   _(‘string_to_translate’,‘text_domain’); _   _e(‘string_to_translate’,‘text_domain’); //Echoed to browser
More Information Writing a Plugin: http://codex.wordpress.org/Writing_a_Plugin Plugin API http://codex.wordpress.org/Plugin_API Plugin API / Action Reference http://codex.wordpress.org/Plugin_API/Action_Reference Plugin API / Filter Reference http://codex.wordpress.org/Plugin_API/Filter_Reference

More Related Content

PDF
Jumping Into WordPress Plugin Programming
KEY
Actions filters
PPTX
Plugin development wpmeetup010
PDF
Django cms best practices
KEY
Writing your Third Plugin
PDF
Bending word press to your will
PDF
PloneNG: What's new in Plone 4.2, 4.3, and beyond
KEY
WordPress Bootcamp Part 2 - Extending WordPress
Jumping Into WordPress Plugin Programming
Actions filters
Plugin development wpmeetup010
Django cms best practices
Writing your Third Plugin
Bending word press to your will
PloneNG: What's new in Plone 4.2, 4.3, and beyond
WordPress Bootcamp Part 2 - Extending WordPress

What's hot (20)

PPTX
Plug in development
PPTX
Top Wordpress dashboard hacks
PDF
Basic Crud In Django
PDF
Getting Ignited with EE2
PDF
Flask patterns
ODP
The Future Of WordPress Presentation
ODP
PHP Basic
PPT
Introduction To Php For Wit2009
PPSX
Php and MySQL
PPT
Introduction to PHP
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
PPT
Short Intro to PHP and MySQL
PDF
Creating native apps with WordPress
PPT
PHP - Introduction to PHP Functions
PDF
Object Oriented Programming for WordPress Plugin Development
PDF
How To Structure Go Applications - Paul Bellamy - Codemotion Milan 2016
ODP
Advanced Perl Techniques
PPT
LPW: Beginners Perl
Plug in development
Top Wordpress dashboard hacks
Basic Crud In Django
Getting Ignited with EE2
Flask patterns
The Future Of WordPress Presentation
PHP Basic
Introduction To Php For Wit2009
Php and MySQL
Introduction to PHP
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Short Intro to PHP and MySQL
Creating native apps with WordPress
PHP - Introduction to PHP Functions
Object Oriented Programming for WordPress Plugin Development
How To Structure Go Applications - Paul Bellamy - Codemotion Milan 2016
Advanced Perl Techniques
LPW: Beginners Perl
Ad

Viewers also liked (20)

PPSX
WordPress Theme Design and Development Workshop - Day 2
PDF
Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...
PPTX
Custom Database Queries in WordPress
ODP
Top 10 WordPress Plugins
PPTX
To build a WordPress Theme: Wordcamp Denmark 2014
PDF
Federal reserve
PPTX
Build a WordPress theme from HTML5 template @ Telerik
PPTX
10 Must Have WordPress Plugins
PPTX
Analisis Kinerja Reksadana Saham Syariah Menggunakan Metode Sharpe, Treynor, ...
PDF
WordPress Database: What's behind those 12 tables
ODP
Hands On Approach To Networking
PPTX
PEDOMAN TEKNIS TATA CARA PEMOTONGAN, PENYETORAN DAN PELAPORAN PAJAK PENGHASIL...
PPT
WordPress Theme Design - Rich Media Institute Workshop
PPTX
Php Vs Phyton
PDF
Tips Pribadi hebat: Mandiri, Sukses, dan Mulia
ODP
PHP Web Programming
PDF
Installing WordPress on AWS
PPT
JavaScript - An Introduction
PDF
WordPress SEO & Optimisation
WordPress Theme Design and Development Workshop - Day 2
Don't Fear the Custom Theme: How to build a custom WordPress theme with only ...
Custom Database Queries in WordPress
Top 10 WordPress Plugins
To build a WordPress Theme: Wordcamp Denmark 2014
Federal reserve
Build a WordPress theme from HTML5 template @ Telerik
10 Must Have WordPress Plugins
Analisis Kinerja Reksadana Saham Syariah Menggunakan Metode Sharpe, Treynor, ...
WordPress Database: What's behind those 12 tables
Hands On Approach To Networking
PEDOMAN TEKNIS TATA CARA PEMOTONGAN, PENYETORAN DAN PELAPORAN PAJAK PENGHASIL...
WordPress Theme Design - Rich Media Institute Workshop
Php Vs Phyton
Tips Pribadi hebat: Mandiri, Sukses, dan Mulia
PHP Web Programming
Installing WordPress on AWS
JavaScript - An Introduction
WordPress SEO & Optimisation
Ad

Similar to WordPress Plugin Basics (20)

PPT
Developing WordPress Plugins
KEY
Plugin Development Practices
PPTX
WordPress Structure and Best Practices
PDF
Write your first WordPress plugin
PPTX
WordPress Plugin development
PDF
Intro to WordPress Plugin Development
PPTX
Introduction to Plugin Programming, WordCamp Miami 2011
KEY
How To Write a WordPress Plugin
PDF
Creating Your First WordPress Plugin
PPT
Developing Plugins For WordPress
PPTX
WordPress plugin #2
PPT
WordPress basic fundamental of plugin development and creating shortcode
PDF
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
PDF
Introduction to WordPress Hooks 2016
PPTX
Getting Started With WordPress Development
PPTX
Intro to Plugin Development, Miami WordCamp, 2015
PDF
Plugin development demystified 2017
PDF
Step by step guide for creating wordpress plugin
PPTX
Childthemes ottawa-word camp-1919
PPT
Word press Plugins by WordPress Experts
Developing WordPress Plugins
Plugin Development Practices
WordPress Structure and Best Practices
Write your first WordPress plugin
WordPress Plugin development
Intro to WordPress Plugin Development
Introduction to Plugin Programming, WordCamp Miami 2011
How To Write a WordPress Plugin
Creating Your First WordPress Plugin
Developing Plugins For WordPress
WordPress plugin #2
WordPress basic fundamental of plugin development and creating shortcode
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Introduction to WordPress Hooks 2016
Getting Started With WordPress Development
Intro to Plugin Development, Miami WordCamp, 2015
Plugin development demystified 2017
Step by step guide for creating wordpress plugin
Childthemes ottawa-word camp-1919
Word press Plugins by WordPress Experts

More from Amanda Giles (6)

PPTX
The Way to Theme Enlightenment 2017
PPTX
Amp Up Your Admin
PPTX
The Way to Theme Enlightenment
PPTX
Creating Customizable Widgets for Unpredictable Needs
PPTX
Shortcodes vs Widgets: Which one and how?
PPTX
Creating Customizable Widgets for Unpredictable Needs
The Way to Theme Enlightenment 2017
Amp Up Your Admin
The Way to Theme Enlightenment
Creating Customizable Widgets for Unpredictable Needs
Shortcodes vs Widgets: Which one and how?
Creating Customizable Widgets for Unpredictable Needs

Recently uploaded (20)

PPTX
A portfolio Template for Interior Designer
PDF
Top 10 Visionary Entrepreneurs to Watch in 2025
PDF
Anxiety Awareness Journal One Week Preview
PPT
Lesson From Geese! Understanding Teamwork
PPTX
UNIVERSAL HUMAN VALUES for NEP student .pptx
PPTX
show1- motivational ispiring positive thinking
PPTX
Emotional Intelligence- Importance and Applicability
PDF
Want to Fly Like an Eagle - Leave the Chickens Behind.pdf
PPTX
THEORIES-PSYCH-3.pptx theory of Abraham Maslow
PDF
OneRead_20250728_1807.pdfbdjsajaajjajajsjsj
PPTX
Unlocking Success Through the Relentless Power of Grit
PDF
Dominate Her Mind – Make Women Chase, Lust, & Submit
DOCX
Boost your energy levels and Shred Weight
DOCX
Paulo Tuynmam: Nine Timeless Anchors of Authentic Leadership
PDF
⚡ Prepping for grid failure_ 6 Must-Haves to Survive Blackout!.pdf
PDF
The Blogs_ Humanity Beyond All Differences _ Andy Blumenthal _ The Times of I...
PPTX
Commmunication in Todays world- Principles and Barriers
PDF
Quiet Wins: Why the Silent Fish Survives.pdf
PDF
Why is mindset more important than motivation.pdf
PDF
SEX-GENDER-AND-SEXUALITY-LESSON-1-M (2).pdf
A portfolio Template for Interior Designer
Top 10 Visionary Entrepreneurs to Watch in 2025
Anxiety Awareness Journal One Week Preview
Lesson From Geese! Understanding Teamwork
UNIVERSAL HUMAN VALUES for NEP student .pptx
show1- motivational ispiring positive thinking
Emotional Intelligence- Importance and Applicability
Want to Fly Like an Eagle - Leave the Chickens Behind.pdf
THEORIES-PSYCH-3.pptx theory of Abraham Maslow
OneRead_20250728_1807.pdfbdjsajaajjajajsjsj
Unlocking Success Through the Relentless Power of Grit
Dominate Her Mind – Make Women Chase, Lust, & Submit
Boost your energy levels and Shred Weight
Paulo Tuynmam: Nine Timeless Anchors of Authentic Leadership
⚡ Prepping for grid failure_ 6 Must-Haves to Survive Blackout!.pdf
The Blogs_ Humanity Beyond All Differences _ Andy Blumenthal _ The Times of I...
Commmunication in Todays world- Principles and Barriers
Quiet Wins: Why the Silent Fish Survives.pdf
Why is mindset more important than motivation.pdf
SEX-GENDER-AND-SEXUALITY-LESSON-1-M (2).pdf

WordPress Plugin Basics

  • 1. WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email_address]
  • 2. What is a Plugin? WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. Instead of changing the core programming of WordPress, you can add functionality with WordPress Plugins. Here is a basic definition: WordPress Plugin: A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API). Swiped from http://codex.wordpress.org/Writing_a_Plugin
  • 3. Why Write a Plugin? Solve a problem Extend existing functionality Save time Portability (changing themes, using on multiple sites) Make money (???)
  • 4. Plugin Basics 1 or more files placed in the wp-content/plugins folder 1 file should have the header so WP recognizes it Can integrate into the admin as well your site pages and posts Can extend or even remove existing WP functionality (careful!)
  • 5. Plugin Header One of your plugin files must have this header for WP to recognize it: <?php /* Plugin Name: Header and Footer Plugin URI: http://www.satollo.net/plugins/header-footer Description: Lets you to add code to the head and footer of pages. Author: Satollo Author URI: http://www.satollo.net */ ?>
  • 6. Plugin License Most WordPress plugins use the GPL2 license also used by WordPress. To use it, include the following in your plugin file: <?php /* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?>
  • 7. Plugin Code May Include: Hooks to Actions or Filters Hooks define how your plugin interacts with WordPress Functions Defined Constants New Admin pages & saved options
  • 8. Actions hooks are triggered by events in WP such as wp_head , wp_footer , admin_init , register_user , comment_post Syntax is: P=Priority (numeric, defaults to 10, lower the number the earlier it executes) N=Number of arguments the function can accept Example: Action Hooks add_action(‘hook-name’, ‘function-name’) add_action(‘hook-name’, ‘function-name’, P, N) add_action( 'admin_notices', 'hello_dolly' ); function hello_dolly() { $chosen = hello_dolly_get_lyric(); echo &quot;<p id='dolly'>$chosen</p>&quot;; }
  • 9. Filter Hooks Filter hooks are used to modify text before adding it to the database or displaying it on screen – examples include the_content , the_title , posts_where Syntax is: P=Priority (numeric, defaults to 10, lower the number the earlier it executes) N=Number of arguments the function can accept Example: add_filter(‘hook-name’, ‘function-name’); add_filter(‘hook-name’, ‘function-name’, P, N); add_filter( ‘the_title', ‘star_titles' );
  • 10. Filter Functions A filter is altering text (or potentially altering text) Therefore, a filter function is receiving text as a parameter and must return text as a return value Example: add_filter( ‘the_title', ‘star_titles' ); function star_titles($title) { return ‘*** ‘ . $title . ‘ ***’; }
  • 11. Removing WP Functionality Sometimes a plugin is designed to remove something WP is already doing Syntax is: Obviously, be very careful when doing this! remove_action(‘hook-name’, ‘function-name’); remove_filter(‘hook-name’, ‘function-name’);
  • 12. Changes to WP Admin Add an Options page within Admin Save a new plugin option in WP Database: Update an option in WP Database: (even someone else’s): Retrieve an option from WP Database: add_options_page(‘page_title’,‘menu_title’,‘capability’,’menu-slug’,’function-name’); add_options_page(‘Footer Edit’,‘Footer Edit’,‘manage_options’,’footer-edit’, ’ footer-edit/footer.php’); add_option(‘option_name’,‘option_value’); add_option(‘footer_text’,‘Copyright 2011’); update_option(‘option_name’,‘option_value’); get_option(‘option_name’);
  • 13. Saving Data For small bits of data to be saved, using the WP options table works well Data writes are time consuming. To save time writing and pulling data, can combine bits of data into an array. add_option(‘first_name’,‘Jane’); add_option(‘middle_name’,‘Ann’); add_option(‘last_name’,‘Smith’); $name_arr = array( “ first_name” => ‘Jane’, “ middle_name” => ‘Ann’, “ last_name” => ‘Smith’); add_option(‘full_name’, $name_arr ); Can be written as What if your plugin requires much more data to be stored in the database???
  • 14. Creating new database tables Define table name: global $wpdb; $table_name = $wpdb->prefix . “contributor_names&quot;; Check if table already exists: if($wpdb->get_var(&quot;SHOW TABLES LIKE '$table_name'&quot;) != $table_name) Add table using dbDelta function: $sql = &quot;CREATE TABLE &quot; . $table_name . &quot; ( id mediumint(9) NOT NULL AUTO_INCREMENT, first_name tinytext NOT NULL, middle_name tinytext NOT NULL, last_name tinytext NOT NULL, url VARCHAR(200) NOT NULL, UNIQUE KEY id (id) );&quot;; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql);
  • 15. Other Considerations Ensure your plugin has a unique folder name (or unique file names if not in a folder) Use unique function names. Having a uniform prefix for all your functions can help with this. Very important! WP Version Checking within code, or check if specific WP function exists before calling Internationalization Licensing Documentation, Comments, or ReadMe.txt if ( function_exists ( ' has_term') )
  • 16. Internationalization Goal is to mark strings which can be translated (even if no current translation exists) Good idea if you plan on distributing your plugin Translate String syntax: Function name begins with 2 underscores back to back The ‘text_domain’ is a unique identifier, which makes sure WP can distinguish between all loaded translations. Using the basename of your plugin is always a good choice. _ _(‘string_to_translate’); _ _e(‘string_to_translate’); //Echoed to browser _ _(‘string_to_translate’,‘text_domain’); _ _e(‘string_to_translate’,‘text_domain’); //Echoed to browser
  • 17. More Information Writing a Plugin: http://codex.wordpress.org/Writing_a_Plugin Plugin API http://codex.wordpress.org/Plugin_API Plugin API / Action Reference http://codex.wordpress.org/Plugin_API/Action_Reference Plugin API / Filter Reference http://codex.wordpress.org/Plugin_API/Filter_Reference