Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started with WordPress Plugin Development

Getting Started with WordPress Plugin Development

My first conference talk given at WordUpNess on 3rd Nov 2012. List of links can be found at http://cl.ly/Kesi

Gilbert Pellegrom

November 03, 2012
Tweet

Other Decks in Technology

Transcript

  1. Plugin Development Experience Nivo Slider http://nivo.dev7studios.com $200,000+ in 16 months

    Showcase http://showcase.dev7studios.com 7 WordPress.org plugins 20,000+ downloads WP Updates http://wp-updates.com
  2. Why build a plugin? To customise, modify or enhance your

    WordPress site Add functionality without editing the core Modular package that can be updated independently
  3. Plugin vs Theme Plugin code is “theme” independent Freedom to

    enable/disable as required No performance difference Plugins are harder to implement General rule: Design = Theme / Functionality = Plugin
  4. Plugin Meta <?php /* Plugin Name: Name Of The Plugin

    Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates Description: A brief description of the Plugin. Version: The Plugin's Version Number, e.g.: 1.0 Author: Name Of The Plugin Author Author URI: http://URI_Of_The_Plugin_Author License: A "Slug" license name e.g. GPL2 */ ?>
  5. readme.txt === Plugin Name === Contributors: markjaquith, mdawaffe (this should

    be a list of wordpress.org userid's) Donate link: http://example.com/ Tags: comments, spam Requires at least: 3.0.1 Tested up to: 3.4 Stable tag: 4.3 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Here is a short description of the plugin. This should be no more than 150 characters. No markup here. == Description == This is the long description...
  6. Plugin API: Actions & Filters Actions are the hooks that

    the WordPress core launches at specific points during execution, or when specific events occur. 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.
  7. Plugin API: Actions & Filters // Example action function email_friends(

    $post_ID ) { $friends = '[email protected], [email protected]'; wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' ); return $post_ID; } add_action( 'publish_post', 'email_friends' );
  8. Plugin API: Actions & Filters // Example filter function updated_messages(

    $messages ) { global $post, $post_ID; $showcase_messages = array( 0 => '', // Unused. Messages start at index 1. 1 => __('Showcase Gallery updated.', 'showcase'), 2 => __('Custom field updated.', 'showcase'), 3 => __('Custom field deleted.', 'showcase'), 4 => __('Showcase Gallery updated.', 'showcase'), //... ); return $messages; } add_filter( 'post_updated_messages', 'updated_messages' );
  9. Shortcodes & Template Tags // Example shortcode [footag foo="bar"] function

    footag_func($atts) { return "foo = {$atts[foo]}"; } add_shortcode('footag', 'footag_func'); // Example template tag <?php wp_nav_menu( $args ); ?>
  10. Options & Settings Save some data to the database: add_option(

    'my_option', $value ); Get the data from the database: get_option( 'my_option' ); Check out the Settings API
  11. A Note About Names PREFIX EVERYTHING!!! e.g. dev7_my_func(){ ... }

    Enclose plugin functions in a class class MyAwesomePlugin { function __construct() { add_action( 'init', array(&$this, 'init') ); } function init() { ... } } new MyAwesomePlugin();
  12. Useful Functions & the Codex http://codex.wordpress.org The Codex is your

    friends WordPress is packaged with loads of useful functions Learn to search the Codex
  13. Licensing Generally WordPress plugins should be GLP2 or GLP2 compatible.

    Assets (JS/CSS) can be copyrighted <?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 */ ?>
  14. Submitting Your Plugin to the WordPress.org Repository Sign up at

    wordpress.org Visit http://wordpress.org/extend/ plugins/add/ Once approved you get a SVN repo See Developer Center for help using Subversion