SlideShare a Scribd company logo
WordPress Plugin Development 201

             Yannick Lefebvre
                Plugin Developer


                       @ylefebvre
                       ylefebvre.ca
         profiles.wordpress.org/users/jackdewey/



              Presentation available at:
       yannickcorner.nayanna.biz/wcmtl2012
WordPress Plugin Development 201

•   Introduction
•   Recap: Plugins Overview
•   Setting up a local development environment
•   Creating help tabs
•   Loading and using jQuery safely
•   Internationalization
•   Enhancing your plugin page on wordpress.org
Introduction

●   Using WordPress since 2004
●   Released first plugin (Link Library) in 2005
●   Released 8 Plugins to date on the official repository
Introduction

●   Published WordPress Plugin
    Development Cookbook with Packt
    Publishing in July 2012
Introduction

    ●    Published WordPress Plugin
         Development Cookbook with Packt
         Publishing in July 2012
                      Contest!
●   Tweet @ylefebvre with hashtag
    #wpplugincookbook before 10:45am
    for a chance to win one of three (3)
    copies.
        Or use: http://clicktotweet.com/rKH_c
●   Get 40% discount on e-book with code
    wcmontreal12 at
    http://link.packtpub.com/xyVYFw
Recap: Plugins Overview

•    Allow developers to extend default WordPress capabilities
    (on hosted installations, not on .com)
•   Open plugin architecture present since very first versions
•   Plugin API constantly refined and expanded
•   Plugin code size and complexity vary widely from one to
    another
•   Functionality stays in place when theme is changed
•   Can be installed directly from WordPress admin or through a
    manual upload and activation process




                                   http://clicktotweet.com/rKH_c
Recap: Plugins Overview

●   Made from one or more php code file(s)
●   Can optionally contain other file types (e.g. images, text
    files, translation files, etc...)
●   Located directly in the wp-contentplugins directory or in
    a sub-directory within the plugins folder
●   Entry point is a .php file that contains a specific plugin
    header at its top




                                   http://clicktotweet.com/rKH_c
Recap: Plugins Overview

●   The power of plugins comes from their ability to
    register custom functions to be called at specific points
    during the execution of WordPress
●   This process is called hooking
●   Two types of hooks
    –   Action hooks allow for code to be executed at a specific
        point during the page processing loop (registered using the
        add_action function)
    –   Filter hooks are called during WordPress data processing
        to allow plugins to modify, increase or reduce data before it
        is displayed (registered using the add_action function)


                                      http://clicktotweet.com/rKH_c
Recap: Plugins Overview

●   Full recap from last year's presentation:
    –   http://wordpress.tv/2011/08/16/yannick-lefebvre-
        plugin-development-demystified/




                                http://clicktotweet.com/rKH_c
Setting up a local development environment

●   Running all tools that are on a web server on your personal
    computer
●   There are many benefits to working on a local environment:
    •   No risk of breaking a live installation (WPOD)
    •   No need to constantly upload files to a remote server
    •   Faster page refresh with all requests running locally
    •   More control over web server configuration




                                    http://clicktotweet.com/rKH_c
Suggested Toolbox

Web Server                 Subversion Client
                                            ●   TortoiseSVN
                                                (Windows)


   Windows / Mac / Linux
                                            ●   Cornerstone (Mac)
WordPress

                                            ●   Versions (Mac)




                           http://clicktotweet.com/rKH_c
Suggested Toolbox (cont)

Code / Text Editor         Integrated Development
                           Environment (IDE)




         Windows
                                  Windows / Mac / Linux




       Sublime (Mac)




                           http://clicktotweet.com/rKH_c
Programmer's Notepad




                       http://clicktotweet.com/rKH_c
NetBeans IDE
Creating Help Tabs

●   All good plugins should provide documentation to
    their users to facilitate installation
●   Readme files packed with the plugin or instructions
    on the official plugin repository are not efficient as
    users don't seek these out
●   New multi-tab help system allows plugin developers
    to build elaborate help that is accessible from a
    plugin's admin pages




                                http://clicktotweet.com/rKH_c
Creating Help Tabs




                     http://clicktotweet.com/rKH_c
Creating Help Tabs




                     http://clicktotweet.com/rKH_c
Creating Help Tabs

How to do it:
$options_page = add_options_page('My Google Analytics
Configuration', 'My Google Analytics', 'manage_options',
'my-google-analytics', 'ga_config_page' );




                              http://clicktotweet.com/rKH_c
Creating Help Tabs

How to do it:
$options_page = add_options_page('My Google Analytics
Configuration', 'My Google Analytics', 'manage_options',
'my-google-analytics', 'ga_config_page' );
if ( $options_page )
   add_action( 'load-' . $options_page,
                'ga_help_tabs' );




                              http://clicktotweet.com/rKH_c
Creating Help Tabs
function ga_help_tabs() {
    $screen = get_current_screen();
    $screen->add_help_tab(
             array( 'id' => 'ga-plugin-help-instructions',
                    'title' => 'Instructions',
                    'callback' => 'ga_plugin_help_instructions' ) );


    $screen->add_help_tab(
             array( 'id' => 'ga-plugin-help-faq',
                    'title' => 'FAQ',
                    'callback' => 'ga_plugin_help_faq' ) );


    $screen->set_help_sidebar( '<p>This is the sidebar content</p>' );
}
Creating Help Tabs
function ga_plugin_help_instructions() { ?>
    <p>These are instructions explaining how to use this plugin.</p>
<?php }


function ga_plugin_help_faq() { ?>
    <p>These are the most frequently asked questions on the use of this
plugin.</p>
<?php }




                                      http://clicktotweet.com/rKH_c
Creating Help Tabs




                     http://clicktotweet.com/rKH_c
Loading and using jQuery safely

●   JavaScript and jQuery can bring great interactivity to
    web sites
●   They can also quickly bring a site to a halt when
    conflicts occur between multiple versions of a script
    or errors in a single script.
●   WordPress includes mechanisms to help avoid these
    conflicts
●   The following technique applies to plugin AND theme
    developers alike



                               http://clicktotweet.com/rKH_c
Loading and using jQuery safely

●   Three action hooks are now provided to register
    script and style requests:
    –   wp_enqueue_scripts (for regular visitor-facing
        pages)
    –   admin_enqueue_scripts (for admin panel
        pages)
    –   login_enqueue_scripts (for login page)




                              http://clicktotweet.com/rKH_c
Loading and using jQuery safely

●   Upon callback, a single function call takes care of loading
    the scripts that are provided with WordPress:
    wp_enqueue_script( 'jquery' );


●   WordPress will analyze all requests and boil them down to
    loading a single instance of each script
●   Many other scripts can be loaded using same technique
    (Scriptaculous, ThickBox, TinyMCE, etc...)
●   Third-party scripts can be loaded with same function, with
    more parameters to indicate script location (see Codex for
    full list)

                                  http://clicktotweet.com/rKH_c
Example displaying a pop-up dialog using the
built-in ThickBox script
add_action( 'wp_enqueue_scripts',
           'pud_load_scripts' );



function pud_load_scripts() {
    wp_enqueue_script( 'jquery' );
    add_thickbox();
}


add_action( 'wp_footer', 'pud_footer_code' );




                                http://clicktotweet.com/rKH_c
Example using the built-in ThickBox script
function pud_footer_code() { ?>
    <script type="text/javascript">
      jQuery( document ).ready(function() {
          setTimeout( function() {
             tb_show( 'Pop-Up Message',
                '<?php echo plugins_url(
                'content.html?width=420&height=220',
                __FILE__ )?>', null );
          }, 2000 );
      } );
    </script>
<?php }



                                     http://clicktotweet.com/rKH_c
Example using the built-in ThickBox script




                          http://clicktotweet.com/rKH_c
Internationalization

●   Enables plugins to be translated to any language
●   Initial setup work needs to be done by plugin
    developer to make plugin code compatible with
    mechanism
●   Any user can create a local plugin translation and
    use it locally or submit their work for inclusion in
    future plugin updates




                                http://clicktotweet.com/rKH_c
Internationalization

●   Key Functions
    –   _e: Looks up translation string for text and echoes
        result to browser
    –   __: Two underscores. Same as previous but returns a
        string instead of displaying it directly
●   Parameters are text in default language and name of
    text domain
<h2><?php _e( 'Account number',
                'my-google-analytics' ); ?>




                                  http://clicktotweet.com/rKH_c
Internationalization

Admin code before internationalization:
function my_new_plugin_show_admin() {
     $options = get_option('NewGA_Options');
?>
<h1>New Google Analytics Plugin</h1>
<form name="newgaform" method="post" action="">
GA User ID:
<input type="text" name="gauser" value="<?php echo $options['gauser']; ?>"/><br />
<input type="submit" value="Submit" />
</form>
<?php }




                                               http://clicktotweet.com/rKH_c
Internationalization

Admin code after internationalization:
function my_new_plugin_show_admin() {
     $options = get_option( 'NewGA_Options' );
?>
<h1><?php _e( 'New Google Analytics Plugin', 'my-google-analytics' ); ?></h1>
<form name="newgaform" method="post" action="">
<?php _e( 'GA User ID', 'my-google-analytics' ); ?>:
<input type="text" name="gauser" value="<?php echo $options['gauser']; ?>"/><br />
<input type="submit" value="<?php _e( 'Submit', 'my-google-analytics' ); ?>" />
</form>
<?php }




                                           http://clicktotweet.com/rKH_c
Internationalization

●   After making calls to _e and __ throughout your
    plugin's code, translation file can be created using
    Poedit




                                http://clicktotweet.com/rKH_c
Internationalization

●   Translated text domain is loaded using API function
    on plugin initialization

add_action( 'init', 'my_google_analytics_init' );


function my_google_analytics_init() {
      load_plugin_textdomain( 'my-google-analytics',
                             false,
    dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

●   Default text is shown if no translated text
    domain is found for user's selected language
Internationalization

●   While tempting, avoid using variable or PHP
    declarations to hold strings to be translated, as that
    will conflict with the translation lookup mechanism.
●   Punctuation can be included in the text to be
    translated since it might change places in different
    languages.
●   More advanced functions can also be used for
    internationalization:
    –   _n (singular vs plural), _x (translate with context),
    –   _ex, _nx

                                 http://clicktotweet.com/rKH_c
Enhancing your plugin page on wordpress.org

●   Since December 2011, plugins submitted to the
    official repository are able to customize their page
    with an image




                                http://clicktotweet.com/rKH_c
Enhancing your plugin page on wordpress.org

●   Since December 2011, plugins submitted to the
    official repository are able to customize their page
    with an image




                                http://clicktotweet.com/rKH_c
Enhancing your plugin page on wordpress.org

●   Banner must be exactly 772 x 250 pixels to fit within
    the wordpress.org layout
●   Banner must have name banner-772x250.png
●   Developer must create a new folder in plugin folder
    on official repository named assets, next to
    branches, tags and trunk and upload image to
    this folder
●   Image should avoid having too much content in
    lower-left corner as that space is used to display
    plugin name


                               http://clicktotweet.com/rKH_c
Recommended Readings

●   WordPress Plugin Development
    Cookbook by Yannick Lefebvre,
    published by Packt Publishing
    (www.packtpub.com)
●   WordPress Codex
    (codex.wordpress.com)
●   PHP.net
●   StackOverflow.com Programming
    Samples
●   Today's presentation and code samples
    available at:
    –   http://yannickcorner.nayanna.biz/wcmtl2012
                                     http://clicktotweet.com/rKH_c
And the winners are...
And the winners are...

●   Thank you for participating!
●   If you did not win, get a 40%
    discount on e-book version with
    code wcmontreal12 when visiting
    http://link.packtpub.com/xyVYFw
Thank you for attending this talk on Plugin
               Development.




               Questions?



                  Presentation:
  http://yannickcorner.nayanna.biz/wcmtl2012
        Contact: ylefebvre@gmail.com
              Twitter: @ylefebvre
              Blog: ylefebvre.ca
Plugins: profiles.wordpress.org/users/jackdewey

More Related Content

PDF
Magento 2 Development
PDF
Creating Your First WordPress Plugin
PDF
Wordpress development: A Modern Approach
PDF
DevHub 3 - Composer plus Magento
PDF
Dependency management in Magento with Composer
ODP
Moodle Development Best Pracitces
PDF
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
PDF
WPDay Bologna 2013
Magento 2 Development
Creating Your First WordPress Plugin
Wordpress development: A Modern Approach
DevHub 3 - Composer plus Magento
Dependency management in Magento with Composer
Moodle Development Best Pracitces
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
WPDay Bologna 2013

What's hot (14)

PDF
Developers, Be a Bada$$ with WP-CLI
PDF
A Successful Magento Project From Design to Deployment
PDF
ENIB 2015-2016 - CAI Web - S01E01- La forge JavaScript
PDF
Using Drupal Features in B-Translator
PPTX
How to develope plugin in wordpress: 6 simple steps.
PDF
"Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ...
PPTX
Nahlédněte za oponu VersionPressu
PDF
Development Setup of B-Translator
PDF
OpenCms Days 2015 Workflow using Docker and Jenkins
PPTX
Madison PHP - Getting Started with Magento 2
ODP
Openbit szkolenie-drupal-podstawy 2
PDF
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
PDF
B-Translator as a Software Engineering Project
ODP
Developing new feature in Joomla - Joomladay UK 2016
Developers, Be a Bada$$ with WP-CLI
A Successful Magento Project From Design to Deployment
ENIB 2015-2016 - CAI Web - S01E01- La forge JavaScript
Using Drupal Features in B-Translator
How to develope plugin in wordpress: 6 simple steps.
"Paragraphs are more powerful than you can expect" from Vasily Jaremchuk for ...
Nahlédněte za oponu VersionPressu
Development Setup of B-Translator
OpenCms Days 2015 Workflow using Docker and Jenkins
Madison PHP - Getting Started with Magento 2
Openbit szkolenie-drupal-podstawy 2
Developing a Joomla 3.x Component using RAD FOF- Part 2: Front-end + demo - J...
B-Translator as a Software Engineering Project
Developing new feature in Joomla - Joomladay UK 2016
Ad

Viewers also liked (8)

PPTX
A peek into the world of WordPress plugin development
PPTX
Historia de la web
PPTX
Ventajas y-desventajas-de-las-redes-sociales
PDF
WordPress page builders a quick introduction
PDF
WordPress page builders - a new tool to build awesome pages quickly
PPT
Android SDK and PhoneGap
PDF
Plugins by tagomoris #fluentdcasual
PDF
Learning from the Best jQuery Plugins
A peek into the world of WordPress plugin development
Historia de la web
Ventajas y-desventajas-de-las-redes-sociales
WordPress page builders a quick introduction
WordPress page builders - a new tool to build awesome pages quickly
Android SDK and PhoneGap
Plugins by tagomoris #fluentdcasual
Learning from the Best jQuery Plugins
Ad

Similar to WordPress Plugin Development 201 (20)

PDF
Write your first WordPress plugin
PDF
Plugin development demystified 2017
PDF
Diagnosing WordPress: What to do when things go wrong
PDF
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
PPTX
Dolibarr - What's new in 20.0 - DevCamp Montpellier 2024.pptx
PDF
Intro to WordPress Plugin Development
PPTX
Dolibarr - What's new in 18.0 - DevCamp Bordeaux 2023.pptx
PDF
EuroPython 2013 - Python3 TurboGears Training
PDF
The WP Engine Developer Experience. Increased agility, improved efficiency.
PDF
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
ODP
Volunteering at YouSee on Technology Support
PDF
The WordPress developer's toolkit
PPTX
php[world] Magento101
PDF
Xdebug from a to x
PPSX
Extending WordPress
PPTX
WordPress Internationalization and Localization - WordPress Translation Day 3...
PDF
Creating Extensible Plugins for WordPress
PDF
Step by step guide for creating wordpress plugin
PPTX
Building and managing applications fast for IBM i
PDF
Creating a Smooth Development Workflow for High-Quality Modular Open-Source P...
Write your first WordPress plugin
Plugin development demystified 2017
Diagnosing WordPress: What to do when things go wrong
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
Dolibarr - What's new in 20.0 - DevCamp Montpellier 2024.pptx
Intro to WordPress Plugin Development
Dolibarr - What's new in 18.0 - DevCamp Bordeaux 2023.pptx
EuroPython 2013 - Python3 TurboGears Training
The WP Engine Developer Experience. Increased agility, improved efficiency.
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Volunteering at YouSee on Technology Support
The WordPress developer's toolkit
php[world] Magento101
Xdebug from a to x
Extending WordPress
WordPress Internationalization and Localization - WordPress Translation Day 3...
Creating Extensible Plugins for WordPress
Step by step guide for creating wordpress plugin
Building and managing applications fast for IBM i
Creating a Smooth Development Workflow for High-Quality Modular Open-Source P...

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
Teaching material agriculture food technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Getting Started with Data Integration: FME Form 101
PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Tartificialntelligence_presentation.pptx
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Teaching material agriculture food technology
Per capita expenditure prediction using model stacking based on satellite ima...
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Getting Started with Data Integration: FME Form 101
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
Assigned Numbers - 2025 - Bluetooth® Document
“AI and Expert System Decision Support & Business Intelligence Systems”

WordPress Plugin Development 201

  • 1. WordPress Plugin Development 201 Yannick Lefebvre Plugin Developer @ylefebvre ylefebvre.ca profiles.wordpress.org/users/jackdewey/ Presentation available at: yannickcorner.nayanna.biz/wcmtl2012
  • 2. WordPress Plugin Development 201 • Introduction • Recap: Plugins Overview • Setting up a local development environment • Creating help tabs • Loading and using jQuery safely • Internationalization • Enhancing your plugin page on wordpress.org
  • 3. Introduction ● Using WordPress since 2004 ● Released first plugin (Link Library) in 2005 ● Released 8 Plugins to date on the official repository
  • 4. Introduction ● Published WordPress Plugin Development Cookbook with Packt Publishing in July 2012
  • 5. Introduction ● Published WordPress Plugin Development Cookbook with Packt Publishing in July 2012 Contest! ● Tweet @ylefebvre with hashtag #wpplugincookbook before 10:45am for a chance to win one of three (3) copies. Or use: http://clicktotweet.com/rKH_c ● Get 40% discount on e-book with code wcmontreal12 at http://link.packtpub.com/xyVYFw
  • 6. Recap: Plugins Overview • Allow developers to extend default WordPress capabilities (on hosted installations, not on .com) • Open plugin architecture present since very first versions • Plugin API constantly refined and expanded • Plugin code size and complexity vary widely from one to another • Functionality stays in place when theme is changed • Can be installed directly from WordPress admin or through a manual upload and activation process http://clicktotweet.com/rKH_c
  • 7. Recap: Plugins Overview ● Made from one or more php code file(s) ● Can optionally contain other file types (e.g. images, text files, translation files, etc...) ● Located directly in the wp-contentplugins directory or in a sub-directory within the plugins folder ● Entry point is a .php file that contains a specific plugin header at its top http://clicktotweet.com/rKH_c
  • 8. Recap: Plugins Overview ● The power of plugins comes from their ability to register custom functions to be called at specific points during the execution of WordPress ● This process is called hooking ● Two types of hooks – Action hooks allow for code to be executed at a specific point during the page processing loop (registered using the add_action function) – Filter hooks are called during WordPress data processing to allow plugins to modify, increase or reduce data before it is displayed (registered using the add_action function) http://clicktotweet.com/rKH_c
  • 9. Recap: Plugins Overview ● Full recap from last year's presentation: – http://wordpress.tv/2011/08/16/yannick-lefebvre- plugin-development-demystified/ http://clicktotweet.com/rKH_c
  • 10. Setting up a local development environment ● Running all tools that are on a web server on your personal computer ● There are many benefits to working on a local environment: • No risk of breaking a live installation (WPOD) • No need to constantly upload files to a remote server • Faster page refresh with all requests running locally • More control over web server configuration http://clicktotweet.com/rKH_c
  • 11. Suggested Toolbox Web Server Subversion Client ● TortoiseSVN (Windows) Windows / Mac / Linux ● Cornerstone (Mac) WordPress ● Versions (Mac) http://clicktotweet.com/rKH_c
  • 12. Suggested Toolbox (cont) Code / Text Editor Integrated Development Environment (IDE) Windows Windows / Mac / Linux Sublime (Mac) http://clicktotweet.com/rKH_c
  • 13. Programmer's Notepad http://clicktotweet.com/rKH_c
  • 15. Creating Help Tabs ● All good plugins should provide documentation to their users to facilitate installation ● Readme files packed with the plugin or instructions on the official plugin repository are not efficient as users don't seek these out ● New multi-tab help system allows plugin developers to build elaborate help that is accessible from a plugin's admin pages http://clicktotweet.com/rKH_c
  • 16. Creating Help Tabs http://clicktotweet.com/rKH_c
  • 17. Creating Help Tabs http://clicktotweet.com/rKH_c
  • 18. Creating Help Tabs How to do it: $options_page = add_options_page('My Google Analytics Configuration', 'My Google Analytics', 'manage_options', 'my-google-analytics', 'ga_config_page' ); http://clicktotweet.com/rKH_c
  • 19. Creating Help Tabs How to do it: $options_page = add_options_page('My Google Analytics Configuration', 'My Google Analytics', 'manage_options', 'my-google-analytics', 'ga_config_page' ); if ( $options_page ) add_action( 'load-' . $options_page, 'ga_help_tabs' ); http://clicktotweet.com/rKH_c
  • 20. Creating Help Tabs function ga_help_tabs() { $screen = get_current_screen(); $screen->add_help_tab( array( 'id' => 'ga-plugin-help-instructions', 'title' => 'Instructions', 'callback' => 'ga_plugin_help_instructions' ) ); $screen->add_help_tab( array( 'id' => 'ga-plugin-help-faq', 'title' => 'FAQ', 'callback' => 'ga_plugin_help_faq' ) ); $screen->set_help_sidebar( '<p>This is the sidebar content</p>' ); }
  • 21. Creating Help Tabs function ga_plugin_help_instructions() { ?> <p>These are instructions explaining how to use this plugin.</p> <?php } function ga_plugin_help_faq() { ?> <p>These are the most frequently asked questions on the use of this plugin.</p> <?php } http://clicktotweet.com/rKH_c
  • 22. Creating Help Tabs http://clicktotweet.com/rKH_c
  • 23. Loading and using jQuery safely ● JavaScript and jQuery can bring great interactivity to web sites ● They can also quickly bring a site to a halt when conflicts occur between multiple versions of a script or errors in a single script. ● WordPress includes mechanisms to help avoid these conflicts ● The following technique applies to plugin AND theme developers alike http://clicktotweet.com/rKH_c
  • 24. Loading and using jQuery safely ● Three action hooks are now provided to register script and style requests: – wp_enqueue_scripts (for regular visitor-facing pages) – admin_enqueue_scripts (for admin panel pages) – login_enqueue_scripts (for login page) http://clicktotweet.com/rKH_c
  • 25. Loading and using jQuery safely ● Upon callback, a single function call takes care of loading the scripts that are provided with WordPress: wp_enqueue_script( 'jquery' ); ● WordPress will analyze all requests and boil them down to loading a single instance of each script ● Many other scripts can be loaded using same technique (Scriptaculous, ThickBox, TinyMCE, etc...) ● Third-party scripts can be loaded with same function, with more parameters to indicate script location (see Codex for full list) http://clicktotweet.com/rKH_c
  • 26. Example displaying a pop-up dialog using the built-in ThickBox script add_action( 'wp_enqueue_scripts', 'pud_load_scripts' ); function pud_load_scripts() { wp_enqueue_script( 'jquery' ); add_thickbox(); } add_action( 'wp_footer', 'pud_footer_code' ); http://clicktotweet.com/rKH_c
  • 27. Example using the built-in ThickBox script function pud_footer_code() { ?> <script type="text/javascript"> jQuery( document ).ready(function() { setTimeout( function() { tb_show( 'Pop-Up Message', '<?php echo plugins_url( 'content.html?width=420&height=220', __FILE__ )?>', null ); }, 2000 ); } ); </script> <?php } http://clicktotweet.com/rKH_c
  • 28. Example using the built-in ThickBox script http://clicktotweet.com/rKH_c
  • 29. Internationalization ● Enables plugins to be translated to any language ● Initial setup work needs to be done by plugin developer to make plugin code compatible with mechanism ● Any user can create a local plugin translation and use it locally or submit their work for inclusion in future plugin updates http://clicktotweet.com/rKH_c
  • 30. Internationalization ● Key Functions – _e: Looks up translation string for text and echoes result to browser – __: Two underscores. Same as previous but returns a string instead of displaying it directly ● Parameters are text in default language and name of text domain <h2><?php _e( 'Account number', 'my-google-analytics' ); ?> http://clicktotweet.com/rKH_c
  • 31. Internationalization Admin code before internationalization: function my_new_plugin_show_admin() { $options = get_option('NewGA_Options'); ?> <h1>New Google Analytics Plugin</h1> <form name="newgaform" method="post" action=""> GA User ID: <input type="text" name="gauser" value="<?php echo $options['gauser']; ?>"/><br /> <input type="submit" value="Submit" /> </form> <?php } http://clicktotweet.com/rKH_c
  • 32. Internationalization Admin code after internationalization: function my_new_plugin_show_admin() { $options = get_option( 'NewGA_Options' ); ?> <h1><?php _e( 'New Google Analytics Plugin', 'my-google-analytics' ); ?></h1> <form name="newgaform" method="post" action=""> <?php _e( 'GA User ID', 'my-google-analytics' ); ?>: <input type="text" name="gauser" value="<?php echo $options['gauser']; ?>"/><br /> <input type="submit" value="<?php _e( 'Submit', 'my-google-analytics' ); ?>" /> </form> <?php } http://clicktotweet.com/rKH_c
  • 33. Internationalization ● After making calls to _e and __ throughout your plugin's code, translation file can be created using Poedit http://clicktotweet.com/rKH_c
  • 34. Internationalization ● Translated text domain is loaded using API function on plugin initialization add_action( 'init', 'my_google_analytics_init' ); function my_google_analytics_init() { load_plugin_textdomain( 'my-google-analytics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } ● Default text is shown if no translated text domain is found for user's selected language
  • 35. Internationalization ● While tempting, avoid using variable or PHP declarations to hold strings to be translated, as that will conflict with the translation lookup mechanism. ● Punctuation can be included in the text to be translated since it might change places in different languages. ● More advanced functions can also be used for internationalization: – _n (singular vs plural), _x (translate with context), – _ex, _nx http://clicktotweet.com/rKH_c
  • 36. Enhancing your plugin page on wordpress.org ● Since December 2011, plugins submitted to the official repository are able to customize their page with an image http://clicktotweet.com/rKH_c
  • 37. Enhancing your plugin page on wordpress.org ● Since December 2011, plugins submitted to the official repository are able to customize their page with an image http://clicktotweet.com/rKH_c
  • 38. Enhancing your plugin page on wordpress.org ● Banner must be exactly 772 x 250 pixels to fit within the wordpress.org layout ● Banner must have name banner-772x250.png ● Developer must create a new folder in plugin folder on official repository named assets, next to branches, tags and trunk and upload image to this folder ● Image should avoid having too much content in lower-left corner as that space is used to display plugin name http://clicktotweet.com/rKH_c
  • 39. Recommended Readings ● WordPress Plugin Development Cookbook by Yannick Lefebvre, published by Packt Publishing (www.packtpub.com) ● WordPress Codex (codex.wordpress.com) ● PHP.net ● StackOverflow.com Programming Samples ● Today's presentation and code samples available at: – http://yannickcorner.nayanna.biz/wcmtl2012 http://clicktotweet.com/rKH_c
  • 40. And the winners are...
  • 41. And the winners are... ● Thank you for participating! ● If you did not win, get a 40% discount on e-book version with code wcmontreal12 when visiting http://link.packtpub.com/xyVYFw
  • 42. Thank you for attending this talk on Plugin Development. Questions? Presentation: http://yannickcorner.nayanna.biz/wcmtl2012 Contact: [email protected] Twitter: @ylefebvre Blog: ylefebvre.ca Plugins: profiles.wordpress.org/users/jackdewey