SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Downloaden Sie, um offline zu lesen
Yannick Lefebvre
@ylefebvre
ylefebvre.ca
WordPress Plugin Developer / Author
Plugin Development Demystified
Second Edition
Topics
●
Plugins Overview
●
Types of plugin developers
●
Plugins vs functions.php
●
File Structure
●
Required Tools
●
Plugin file header
●
Turning functions.php code
into a plugin
●
Actions hook
●
Filter hook
●
Shortcodes
●
More tools
●
Considerations
●
Publishing your plugin
●
Recommended Readings
●
Questions
Plugin Development Demystified
Slides at ylefebvre.ca/wcmtl2017
About me
●
WordPress user since 2004
●
Released first plugin in 2005
●
8 plugins on oficial repository
●
Author of WordPress Plugin
Development Cookbook, Second
Edition
– Use code WPDCSEd50 to get
50% of eBook on
packtpub.com until Sept 12
●
Custom plugin development
Slides at ylefebvre.ca/wcmtl2017
Plugins Overview
●
Extend WordPress capabilities
●
Open plugin architecture
●
Ofer function of varying complexity
●
More than 50,000 plugins available today!
Plugins Overview
●
Extend WordPress capabilities
●
Open plugin architecture
●
Ofer function of varying complexity
●
More than 50,000 plugins available today!
Who makes
these plugins
and what
tools do they
use to make
them?
Types of plugin developers
Site administrator Website developer
Community developerFreelance developer
Plugins vs functions.php
●
Ever written your own plugin?
Plugins vs functions.php
●
Ever written your own plugin?
●
Ever added code to functions.php on your own site or in a
customer project?
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri(); ?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri(); ?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
Plugins vs functions.php
●
Ever written your own plugin?
functions.php code is 99%of the way towards making a
plugin with 0%of the benefits
●
Ever added code to functions.php on your own site or in a
customer project?
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri(); ?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri(); ?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
Plugins vs functions.php
Benefit functions.php Plugin
Easily activated or deactivated
without need to search or risk of
afecting other code
Theme-independent
Easy to update in customer
installations
Basic Plugin File Structure
●
One or more plain text php code file(s)
●
Can contain other file types (e.g. images,
text files, translation files, etc…)
●
Distributed as zip file
Required Tools
Required
●
Text Editor (Notepad)
Required Tools
Optional
●
Code editor (e.g. Notepad++, Programmer’s
Notepad, Sublime Text)
Required Tools
Optional
●
CodeIDE (PhpStorm)
Required Tools
Optional
●
Image Editor
●
Local web server
– VVV – Varying Vagrant Vagrants
– XAMPP
– MAMP
– WSL – Windows Subsystem for Linux)
●
Archive Tool
Plugin File Header
<?php
/*
Plugin Name: My New Google Analytics Plugin
Plugin URI: http://ylefebvre.ca
Description: New revolutionary GA Plugin
Version: 1.0
Author: Yannick Lefebvre
Author URI: http://ylefebvre.ca
License: GPL2
*/
<?php
/*
Plugin Name: My New Google Analytics Plugin
Plugin URI: http://ylefebvre.ca
Description: New revolutionary GA Plugin
Version: 1.0
Author: Yannick Lefebvre
Author URI: http://ylefebvre.ca
License: GPL2
*/
●
Registers the plugin with WordPress
●
Data visible to users in the Plugins admin section
First plugin sighting
Turning functions.php code into a plugin
<?php
/*
Plugin Name: Add favicon
Plugin URI: http://ylefebvre.ca
Description: Add favicon to site
Version: 1.0
Author: Yannick Lefebvre
Author URI: http://ylefebvre.ca
License: GPL2
*/
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri();?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
<?php
/*
Plugin Name: Add favicon
Plugin URI: http://ylefebvre.ca
Description: Add favicon to site
Version: 1.0
Author: Yannick Lefebvre
Author URI: http://ylefebvre.ca
License: GPL2
*/
function ylefebvre_add_favicon(){ ?>
<link rel="shortcut icon" href="<?php echo
get_stylesheet_directory_uri();?>/images/favicon.ico"/>
<?php }
add_action( 'wp_head', 'ylefebvre_add_favicon' );
Three powerful tools
Shortcodes
Action
hooks
Filter
hooks
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Filter Hook Shortcode
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Event
WordPress is displaying page
header
Response
Execute plugin code to display
additional header content
Filter Hook Shortcode
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Event
WordPress is displaying page
header
Response
Execute plugin code to display
additional header content
Filter Hook
Do you want to modify data
before it is displayed?
Shortcode
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Event
WordPress is displaying page
header
Response
Execute plugin code to display
additional header content
Filter Hook
Do you want to modify data
before it is displayed?
Event
WordPress is preparing posts
to be displayed
Response
Execute plugin code to add
Javascript code to all links
Shortcode
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Event
WordPress is displaying page
header
Response
Execute plugin code to display
additional header content
Filter Hook
Do you want to modify data
before it is displayed?
Provide easy-to-use code for
users to add content to site
Event
WordPress is preparing posts
to be displayed
Response
Execute plugin code to add
Javascript code to all links
Shortcode
Three powerful tools
Action Hook
Do you want to do something
when an event occurs?
Event
WordPress is displaying page
header
Response
Execute plugin code to display
additional header content
Filter Hook
Do you want to modify data
before it is displayed?
Provide easy-to-use code for
users to add content to site
Event
WordPress is preparing posts
to be displayed
Response
Execute plugin code to add
Javascript code to all links
Shortcode
Event
User has placed shortcode in
page content
Response
Generate content and send
back to WP
Assigning an action hook
add_action( 'hook_name', 'your_function_name',
[priority], [accepted_args] );
Example
add_action( 'wp_head', 'newga_header' );
add_action( 'hook_name', 'your_function_name',
[priority], [accepted_args] );
Example
add_action( 'wp_head', 'newga_header' );
●
Most hook names can be found in WordPress Codex or other
repositories
●
864 action hooks
●
You can learn about hooks by looking into WP source code:
function wp_head() {
do_action( 'wp_head' );
}
function wp_head() {
do_action( 'wp_head' );
}
Full action hook implementation
/* Header code */
add_action( 'wp_head', 'newga_header' );
function newga_header() { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;
i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();
a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;
m.parentNode.insertBefore(a,m)})(window,document,'script',
'https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-0000000-0', 'auto');
ga('send', 'pageview');
</script>
<? }
/* Header code */
add_action( 'wp_head', 'newga_header' );
function newga_header() { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;
i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();
a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;
m.parentNode.insertBefore(a,m)})(window,document,'script',
'https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-0000000-0', 'auto');
ga('send', 'pageview');
</script>
<? }
Full action hook implementation
Assigning a filter hook
add_filter( 'filter_name', 'your_function_name',
[priority], [accepted_args] );
Example
add_filter( 'the_content', 'newga_content_filter' );
add_filter( 'filter_name', 'your_function_name',
[priority], [accepted_args] );
Example
add_filter( 'the_content', 'newga_content_filter' );
●
Receive data that can be modified and returned
●
1674 filter hooks
function the_content($more_link_text = null, $stripteaser = 0)
{
$content = get_the_content($more_link_text, $stripteaser);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;
}
function the_content($more_link_text = null, $stripteaser = 0)
{
$content = get_the_content($more_link_text, $stripteaser);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;
}
Full filter hook implementation
add_filter( 'the_content', 'newga_content_filter' );
function newga_content_filter( $the_content ) {
$new_content = str_replace( 'href',
'onClick="recordOutboundLink( this );return false;" href'
, $the_content );
return $new_content;
}
add_filter( 'the_content', 'newga_content_filter' );
function newga_content_filter( $the_content ) {
$new_content = str_replace( 'href',
'onClick="recordOutboundLink( this );return false;" href'
, $the_content );
return $new_content;
}
●
Add Javascript function call to any links found in content
Full filter hook implementation
Full filter hook implementation
add_action( 'wp_footer', 'footer_analytics_code' );
function footer_analytics_code() { ?>
<script type="text/javascript">
function recordOutboundLink( link ) {
ga( 'send', 'event', 'Outbound Links', 'Click',
link.href, {
'transport': 'beacon',
'hitCallback': function() {
document.location = link.href;
}
} );
}
</script>
<?php }
add_action( 'wp_footer', 'footer_analytics_code' );
function footer_analytics_code() { ?>
<script type="text/javascript">
function recordOutboundLink( link ) {
ga( 'send', 'event', 'Outbound Links', 'Click',
link.href, {
'transport': 'beacon',
'hitCallback': function() {
document.location = link.href;
}
} );
}
</script>
<?php }
Adding a shortcode
●
Simple codes used in a post or page to insert content
– [gallery]
– [gallery id="123" size="medium"]
●
Can be used to output special code before and afer content
– [style1]My text block[/style1]
– Ofen introduced by themes
●
If you repeatedly insert similar code on site, make a
shortcode
Shortcode Implementation
●
Since shortcodes are found anywhere within posts / pages,
they must return their output
add_shortcode( 'dq', 'mysite_div_quote' );
function mysite_div_quote( $atts, $content = null ) {
if ( !empty( $content ) ) {
return '<div class="site_quote" style="text-
align:right">' . $content . '</div>';
}
}
add_shortcode( 'dq', 'mysite_div_quote' );
function mysite_div_quote( $atts, $content = null ) {
if ( !empty( $content ) ) {
return '<div class="site_quote" style="text-
align:right">' . $content . '</div>';
}
}
[dq]This is my text[/dq][dq]This is my text[/dq]
Shortcode Implementation
add_shortcode( 'twitterfeed', 'twitter_embed_shortcode' );
function twitter_embed_shortcode( $args ) {
extract( shortcode_atts( array(
'user_name' => 'ylefebvre'
), $args ) );
if ( !empty( $user_name ) ) {
$output = '<a class="twitter-timeline" href="';
$output .= esc_url( 'https://twitter.com/' .
$user_name );
$output .= '">Tweets by ' . esc_html( $user_name );
$output .= '</a><script async ';
$output .= 'src="//platform.twitter.com/widgets.js"';
$output .= ' charset="utf-8"></script>';
} else {
$output = '';
}
return $output;
}
add_shortcode( 'twitterfeed', 'twitter_embed_shortcode' );
function twitter_embed_shortcode( $args ) {
extract( shortcode_atts( array(
'user_name' => 'ylefebvre'
), $args ) );
if ( !empty( $user_name ) ) {
$output = '<a class="twitter-timeline" href="';
$output .= esc_url( 'https://twitter.com/' .
$user_name );
$output .= '">Tweets by ' . esc_html( $user_name );
$output .= '</a><script async ';
$output .= 'src="//platform.twitter.com/widgets.js"';
$output .= ' charset="utf-8"></script>';
} else {
$output = '';
}
return $output;
}
Shortcode Implementation
●
The resulting shortcode [twitterfeed user_name='WordPress'][twitterfeed user_name='WordPress']
becomes
More tools
Admin menu
More tools
Admin menu Admin pages
More tools
Admin menu Admin pages Extend user editor
More tools
Admin menu Admin pages
Custom post types
Extend user editor
More tools
Admin menu Admin pages
Custom post types
Add custom meta
boxes to any editor
Extend user editor
More tools
Admin menu Admin pages
Custom post types
Add custom meta
boxes to any editor
Extend user editor
New
widgets
More tools
Admin menu Admin pages
Custom post types
Add custom meta
boxes to any editor
Extend user editor
New
widgets
Plugin translation
More tools
●
Store and retrieve plugin settings from site database
●
Query posts
●
Insert styles and scripts in page header and footer
●
Directly access database
Considerations
●
Function names must be diferent from WordPress core
functions and other plugins
●
Entire content is evaluated each time site
is rendered
●
A single error will usually bring down the
entire site
●
Using a local development environment is much safer than
developing on live site
Publishing your plugin
●
wordpress.org oficial repository
– Free plugins only
– Must follow GPL license
– Discoverable from admin plugins page
– Built-in update mechanism
●
Marketplaces (CodeCanyon, MOJO Marketplace, WPEden,
etc…)
– Paid premium plugins
– Need to implement custom update method
– Higher level of support expectation
Conclusions
●
Creating a plugin can be created using very few lines of code
●
When you deliver customer projects, make plugins over
adding code to functions.php
●
Consider distributing your plugins to WordPress community
Recommended Readings
●
WordPress Plugin Development
Cookbook, Second Edition by
Yannick Lefebvre, Packt Publishing
●
WordPress Codex
(codex.wordpress.com)
●
PHP.net
●
StackOverflow.com Programming
Samples
●
Today's presentation and code
samples available at:
– http://ylefebvre.ca/wcmtl2017 WPDCSEd50
Questions?
Thank you for attending this talk on
Plugin Development Demystified
Yannick Lefebvre
@ylefebvre
ylefebvre.ca

Weitere ähnliche Inhalte

Was ist angesagt?

How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.Jay Bharat
 
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 Short Tutorial
Wordpress Plugin Development Short TutorialWordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short TutorialChristos Zigkolis
 
How to WordPress: the basics, part 1
How to WordPress:  the basics, part 1How to WordPress:  the basics, part 1
How to WordPress: the basics, part 1R-Cubed Design Forge
 
Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Joe Querin
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginnersjohnpbloch
 
Beginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentBeginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentAizat Faiz
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin developmentLuc De Brouwer
 
Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Chinmoy Mohanty
 
Creating a Plug-In Architecture
Creating a Plug-In ArchitectureCreating a Plug-In Architecture
Creating a Plug-In Architectureondrejbalas
 
Ako na vlastne WP temy
Ako na vlastne WP temyAko na vlastne WP temy
Ako na vlastne WP temyJuraj Kiss
 
Building mobile applications with DrupalGap
Building mobile applications with DrupalGapBuilding mobile applications with DrupalGap
Building mobile applications with DrupalGapAlex S
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsYameen Khan
 
Developing Plugins For WordPress
Developing Plugins For WordPressDeveloping Plugins For WordPress
Developing Plugins For WordPressLester Chan
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupalsparkfabrik
 
Extension developer secrets - How to make money with Joomla
Extension developer secrets - How to make money with JoomlaExtension developer secrets - How to make money with Joomla
Extension developer secrets - How to make money with JoomlaTim Plummer
 
Plugins at WordCamp Phoenix
Plugins at WordCamp PhoenixPlugins at WordCamp Phoenix
Plugins at WordCamp PhoenixAndrew Ryno
 
Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?Edmund Turbin
 
Wordpress Questions & Answers
Wordpress Questions & AnswersWordpress Questions & Answers
Wordpress Questions & AnswersNicole Dion
 

Was ist angesagt? (20)

How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.How to develope plugin in wordpress: 6 simple steps.
How to develope plugin in wordpress: 6 simple steps.
 
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 Short Tutorial
Wordpress Plugin Development Short TutorialWordpress Plugin Development Short Tutorial
Wordpress Plugin Development Short Tutorial
 
How to WordPress: the basics, part 1
How to WordPress:  the basics, part 1How to WordPress:  the basics, part 1
How to WordPress: the basics, part 1
 
Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginners
 
Beginning WordPress Plugin Development
Beginning WordPress Plugin DevelopmentBeginning WordPress Plugin Development
Beginning WordPress Plugin Development
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin development
 
Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)
 
Creating a Plug-In Architecture
Creating a Plug-In ArchitectureCreating a Plug-In Architecture
Creating a Plug-In Architecture
 
Ako na vlastne WP temy
Ako na vlastne WP temyAko na vlastne WP temy
Ako na vlastne WP temy
 
Building mobile applications with DrupalGap
Building mobile applications with DrupalGapBuilding mobile applications with DrupalGap
Building mobile applications with DrupalGap
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress Experts
 
Developing Plugins For WordPress
Developing Plugins For WordPressDeveloping Plugins For WordPress
Developing Plugins For WordPress
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
 
Extension developer secrets - How to make money with Joomla
Extension developer secrets - How to make money with JoomlaExtension developer secrets - How to make money with Joomla
Extension developer secrets - How to make money with Joomla
 
Plugins at WordCamp Phoenix
Plugins at WordCamp PhoenixPlugins at WordCamp Phoenix
Plugins at WordCamp Phoenix
 
Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?
 
Wordpress Questions & Answers
Wordpress Questions & AnswersWordpress Questions & Answers
Wordpress Questions & Answers
 

Ähnlich wie Plugin Development Demystified: 40-character

Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to DevelopmentWordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to DevelopmentEvan Mullins
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For BegineersM A Hossain Tonu
 
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
 
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!Evan Mullins
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
WordPress Plugin Development 201
WordPress Plugin Development 201WordPress Plugin Development 201
WordPress Plugin Development 201ylefebvre
 
Creating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressCreating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressHristo Chakarov
 
WordPress Internationalization and Localization - WordPress Translation Day 3...
WordPress Internationalization and Localization - WordPress Translation Day 3...WordPress Internationalization and Localization - WordPress Translation Day 3...
WordPress Internationalization and Localization - WordPress Translation Day 3...WordPress Trivandrum
 
5 Steps to Develop a WordPress Plugin From Scratch.pdf
5 Steps to Develop a WordPress Plugin From Scratch.pdf5 Steps to Develop a WordPress Plugin From Scratch.pdf
5 Steps to Develop a WordPress Plugin From Scratch.pdfBeePlugin
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST APICaldera Labs
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfonyFrancois Zaninotto
 
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
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginAndolasoft Inc
 

Ähnlich wie Plugin Development Demystified: 40-character (20)

Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to DevelopmentWordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For Begineers
 
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
 
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
WordCamp Asheville 2017 - So You Wanna Dev? Join the Team!
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
WordPress Plugin Development 201
WordPress Plugin Development 201WordPress Plugin Development 201
WordPress Plugin Development 201
 
Extending WordPress
Extending WordPressExtending WordPress
Extending WordPress
 
Write Your First WordPress Plugin
Write Your First WordPress PluginWrite Your First WordPress Plugin
Write Your First WordPress Plugin
 
Creating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPressCreating Extensible Plugins for WordPress
Creating Extensible Plugins for WordPress
 
WordPress Internationalization and Localization - WordPress Translation Day 3...
WordPress Internationalization and Localization - WordPress Translation Day 3...WordPress Internationalization and Localization - WordPress Translation Day 3...
WordPress Internationalization and Localization - WordPress Translation Day 3...
 
5 Steps to Develop a WordPress Plugin From Scratch.pdf
5 Steps to Develop a WordPress Plugin From Scratch.pdf5 Steps to Develop a WordPress Plugin From Scratch.pdf
5 Steps to Develop a WordPress Plugin From Scratch.pdf
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Faster WordPress Workflows
Faster WordPress WorkflowsFaster WordPress Workflows
Faster WordPress Workflows
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
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
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress Plugin
 

Kürzlich hochgeladen

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Kürzlich hochgeladen (20)

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

Plugin Development Demystified: 40-character

  • 1. Yannick Lefebvre @ylefebvre ylefebvre.ca WordPress Plugin Developer / Author Plugin Development Demystified Second Edition
  • 2. Topics ● Plugins Overview ● Types of plugin developers ● Plugins vs functions.php ● File Structure ● Required Tools ● Plugin file header ● Turning functions.php code into a plugin ● Actions hook ● Filter hook ● Shortcodes ● More tools ● Considerations ● Publishing your plugin ● Recommended Readings ● Questions Plugin Development Demystified Slides at ylefebvre.ca/wcmtl2017
  • 3. About me ● WordPress user since 2004 ● Released first plugin in 2005 ● 8 plugins on oficial repository ● Author of WordPress Plugin Development Cookbook, Second Edition – Use code WPDCSEd50 to get 50% of eBook on packtpub.com until Sept 12 ● Custom plugin development Slides at ylefebvre.ca/wcmtl2017
  • 4. Plugins Overview ● Extend WordPress capabilities ● Open plugin architecture ● Ofer function of varying complexity ● More than 50,000 plugins available today!
  • 5. Plugins Overview ● Extend WordPress capabilities ● Open plugin architecture ● Ofer function of varying complexity ● More than 50,000 plugins available today! Who makes these plugins and what tools do they use to make them?
  • 6. Types of plugin developers Site administrator Website developer Community developerFreelance developer
  • 7. Plugins vs functions.php ● Ever written your own plugin?
  • 8. Plugins vs functions.php ● Ever written your own plugin? ● Ever added code to functions.php on your own site or in a customer project? function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' ); function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' );
  • 9. Plugins vs functions.php ● Ever written your own plugin? functions.php code is 99%of the way towards making a plugin with 0%of the benefits ● Ever added code to functions.php on your own site or in a customer project? function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' ); function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' );
  • 10. Plugins vs functions.php Benefit functions.php Plugin Easily activated or deactivated without need to search or risk of afecting other code Theme-independent Easy to update in customer installations
  • 11. Basic Plugin File Structure ● One or more plain text php code file(s) ● Can contain other file types (e.g. images, text files, translation files, etc…) ● Distributed as zip file
  • 13. Required Tools Optional ● Code editor (e.g. Notepad++, Programmer’s Notepad, Sublime Text)
  • 15. Required Tools Optional ● Image Editor ● Local web server – VVV – Varying Vagrant Vagrants – XAMPP – MAMP – WSL – Windows Subsystem for Linux) ● Archive Tool
  • 16. Plugin File Header <?php /* Plugin Name: My New Google Analytics Plugin Plugin URI: http://ylefebvre.ca Description: New revolutionary GA Plugin Version: 1.0 Author: Yannick Lefebvre Author URI: http://ylefebvre.ca License: GPL2 */ <?php /* Plugin Name: My New Google Analytics Plugin Plugin URI: http://ylefebvre.ca Description: New revolutionary GA Plugin Version: 1.0 Author: Yannick Lefebvre Author URI: http://ylefebvre.ca License: GPL2 */ ● Registers the plugin with WordPress ● Data visible to users in the Plugins admin section
  • 18. Turning functions.php code into a plugin <?php /* Plugin Name: Add favicon Plugin URI: http://ylefebvre.ca Description: Add favicon to site Version: 1.0 Author: Yannick Lefebvre Author URI: http://ylefebvre.ca License: GPL2 */ function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri();?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' ); <?php /* Plugin Name: Add favicon Plugin URI: http://ylefebvre.ca Description: Add favicon to site Version: 1.0 Author: Yannick Lefebvre Author URI: http://ylefebvre.ca License: GPL2 */ function ylefebvre_add_favicon(){ ?> <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri();?>/images/favicon.ico"/> <?php } add_action( 'wp_head', 'ylefebvre_add_favicon' );
  • 20. Three powerful tools Action Hook Do you want to do something when an event occurs? Filter Hook Shortcode
  • 21. Three powerful tools Action Hook Do you want to do something when an event occurs? Event WordPress is displaying page header Response Execute plugin code to display additional header content Filter Hook Shortcode
  • 22. Three powerful tools Action Hook Do you want to do something when an event occurs? Event WordPress is displaying page header Response Execute plugin code to display additional header content Filter Hook Do you want to modify data before it is displayed? Shortcode
  • 23. Three powerful tools Action Hook Do you want to do something when an event occurs? Event WordPress is displaying page header Response Execute plugin code to display additional header content Filter Hook Do you want to modify data before it is displayed? Event WordPress is preparing posts to be displayed Response Execute plugin code to add Javascript code to all links Shortcode
  • 24. Three powerful tools Action Hook Do you want to do something when an event occurs? Event WordPress is displaying page header Response Execute plugin code to display additional header content Filter Hook Do you want to modify data before it is displayed? Provide easy-to-use code for users to add content to site Event WordPress is preparing posts to be displayed Response Execute plugin code to add Javascript code to all links Shortcode
  • 25. Three powerful tools Action Hook Do you want to do something when an event occurs? Event WordPress is displaying page header Response Execute plugin code to display additional header content Filter Hook Do you want to modify data before it is displayed? Provide easy-to-use code for users to add content to site Event WordPress is preparing posts to be displayed Response Execute plugin code to add Javascript code to all links Shortcode Event User has placed shortcode in page content Response Generate content and send back to WP
  • 26. Assigning an action hook add_action( 'hook_name', 'your_function_name', [priority], [accepted_args] ); Example add_action( 'wp_head', 'newga_header' ); add_action( 'hook_name', 'your_function_name', [priority], [accepted_args] ); Example add_action( 'wp_head', 'newga_header' ); ● Most hook names can be found in WordPress Codex or other repositories ● 864 action hooks ● You can learn about hooks by looking into WP source code: function wp_head() { do_action( 'wp_head' ); } function wp_head() { do_action( 'wp_head' ); }
  • 27. Full action hook implementation /* Header code */ add_action( 'wp_head', 'newga_header' ); function newga_header() { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r; i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date(); a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a,m)})(window,document,'script', 'https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-0000000-0', 'auto'); ga('send', 'pageview'); </script> <? } /* Header code */ add_action( 'wp_head', 'newga_header' ); function newga_header() { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r; i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date(); a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a,m)})(window,document,'script', 'https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-0000000-0', 'auto'); ga('send', 'pageview'); </script> <? }
  • 28. Full action hook implementation
  • 29. Assigning a filter hook add_filter( 'filter_name', 'your_function_name', [priority], [accepted_args] ); Example add_filter( 'the_content', 'newga_content_filter' ); add_filter( 'filter_name', 'your_function_name', [priority], [accepted_args] ); Example add_filter( 'the_content', 'newga_content_filter' ); ● Receive data that can be modified and returned ● 1674 filter hooks function the_content($more_link_text = null, $stripteaser = 0) { $content = get_the_content($more_link_text, $stripteaser); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]&gt;', $content); echo $content; } function the_content($more_link_text = null, $stripteaser = 0) { $content = get_the_content($more_link_text, $stripteaser); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]&gt;', $content); echo $content; }
  • 30. Full filter hook implementation add_filter( 'the_content', 'newga_content_filter' ); function newga_content_filter( $the_content ) { $new_content = str_replace( 'href', 'onClick="recordOutboundLink( this );return false;" href' , $the_content ); return $new_content; } add_filter( 'the_content', 'newga_content_filter' ); function newga_content_filter( $the_content ) { $new_content = str_replace( 'href', 'onClick="recordOutboundLink( this );return false;" href' , $the_content ); return $new_content; } ● Add Javascript function call to any links found in content
  • 31. Full filter hook implementation
  • 32. Full filter hook implementation add_action( 'wp_footer', 'footer_analytics_code' ); function footer_analytics_code() { ?> <script type="text/javascript"> function recordOutboundLink( link ) { ga( 'send', 'event', 'Outbound Links', 'Click', link.href, { 'transport': 'beacon', 'hitCallback': function() { document.location = link.href; } } ); } </script> <?php } add_action( 'wp_footer', 'footer_analytics_code' ); function footer_analytics_code() { ?> <script type="text/javascript"> function recordOutboundLink( link ) { ga( 'send', 'event', 'Outbound Links', 'Click', link.href, { 'transport': 'beacon', 'hitCallback': function() { document.location = link.href; } } ); } </script> <?php }
  • 33. Adding a shortcode ● Simple codes used in a post or page to insert content – [gallery] – [gallery id="123" size="medium"] ● Can be used to output special code before and afer content – [style1]My text block[/style1] – Ofen introduced by themes ● If you repeatedly insert similar code on site, make a shortcode
  • 34. Shortcode Implementation ● Since shortcodes are found anywhere within posts / pages, they must return their output add_shortcode( 'dq', 'mysite_div_quote' ); function mysite_div_quote( $atts, $content = null ) { if ( !empty( $content ) ) { return '<div class="site_quote" style="text- align:right">' . $content . '</div>'; } } add_shortcode( 'dq', 'mysite_div_quote' ); function mysite_div_quote( $atts, $content = null ) { if ( !empty( $content ) ) { return '<div class="site_quote" style="text- align:right">' . $content . '</div>'; } } [dq]This is my text[/dq][dq]This is my text[/dq]
  • 35. Shortcode Implementation add_shortcode( 'twitterfeed', 'twitter_embed_shortcode' ); function twitter_embed_shortcode( $args ) { extract( shortcode_atts( array( 'user_name' => 'ylefebvre' ), $args ) ); if ( !empty( $user_name ) ) { $output = '<a class="twitter-timeline" href="'; $output .= esc_url( 'https://twitter.com/' . $user_name ); $output .= '">Tweets by ' . esc_html( $user_name ); $output .= '</a><script async '; $output .= 'src="//platform.twitter.com/widgets.js"'; $output .= ' charset="utf-8"></script>'; } else { $output = ''; } return $output; } add_shortcode( 'twitterfeed', 'twitter_embed_shortcode' ); function twitter_embed_shortcode( $args ) { extract( shortcode_atts( array( 'user_name' => 'ylefebvre' ), $args ) ); if ( !empty( $user_name ) ) { $output = '<a class="twitter-timeline" href="'; $output .= esc_url( 'https://twitter.com/' . $user_name ); $output .= '">Tweets by ' . esc_html( $user_name ); $output .= '</a><script async '; $output .= 'src="//platform.twitter.com/widgets.js"'; $output .= ' charset="utf-8"></script>'; } else { $output = ''; } return $output; }
  • 36. Shortcode Implementation ● The resulting shortcode [twitterfeed user_name='WordPress'][twitterfeed user_name='WordPress'] becomes
  • 38. More tools Admin menu Admin pages
  • 39. More tools Admin menu Admin pages Extend user editor
  • 40. More tools Admin menu Admin pages Custom post types Extend user editor
  • 41. More tools Admin menu Admin pages Custom post types Add custom meta boxes to any editor Extend user editor
  • 42. More tools Admin menu Admin pages Custom post types Add custom meta boxes to any editor Extend user editor New widgets
  • 43. More tools Admin menu Admin pages Custom post types Add custom meta boxes to any editor Extend user editor New widgets Plugin translation
  • 44. More tools ● Store and retrieve plugin settings from site database ● Query posts ● Insert styles and scripts in page header and footer ● Directly access database
  • 45. Considerations ● Function names must be diferent from WordPress core functions and other plugins ● Entire content is evaluated each time site is rendered ● A single error will usually bring down the entire site ● Using a local development environment is much safer than developing on live site
  • 46. Publishing your plugin ● wordpress.org oficial repository – Free plugins only – Must follow GPL license – Discoverable from admin plugins page – Built-in update mechanism ● Marketplaces (CodeCanyon, MOJO Marketplace, WPEden, etc…) – Paid premium plugins – Need to implement custom update method – Higher level of support expectation
  • 47. Conclusions ● Creating a plugin can be created using very few lines of code ● When you deliver customer projects, make plugins over adding code to functions.php ● Consider distributing your plugins to WordPress community
  • 48. Recommended Readings ● WordPress Plugin Development Cookbook, Second Edition by Yannick Lefebvre, Packt Publishing ● WordPress Codex (codex.wordpress.com) ● PHP.net ● StackOverflow.com Programming Samples ● Today's presentation and code samples available at: – http://ylefebvre.ca/wcmtl2017 WPDCSEd50
  • 49. Questions? Thank you for attending this talk on Plugin Development Demystified Yannick Lefebvre @ylefebvre ylefebvre.ca