Plugin Development
WordCamp Norway 2014
Barry Kooij
•

Senior Web Developer @ Yoast	


•

WordPress SEO (Premium),What The File, Sub Posts	


•

Contributor EDD, EDD extensions	


•

Moderator WPNL forum	


•

Twitter: @cageNL
Agenda
•

Presentation	


•

Q&A	


•

Personal Q&A
Let’s get coding!
Hold up!
There more to a good plugin than good code!
Wait, what?
My Setup
My Setup
•

MacBook Pro Retina	


•

PhpStorm	


•

GIT -> GitHub	


•

Vagrant w/ Varying Vagrant Vagrants (VVV) -10up
Debugging
Debugging
define( 'WP_DEBUG', true );

if ( WP_DEBUG ) {

	

 define( 'SCRIPT_DEBUG', true );

	

 define( 'WP_DEBUG_LOG', true );

	

 define( 'WP_DEBUG_DISPLAY', true );

	

 @ini_set( 'display_errors', 1 );

}	

display_errors = On;

error_reporting = E_ALL | E_STRICT;	

Xdebug
WordPress Core
Open source
•

WordPress is open source	


•

No really, literally OPEN SOURCE	


•

The code and documentation is all in your project	


•

Try command clicking a function
API
•

Dashboard Widgets API	


•

Rewrite API	


•

Database API	


•

Settings API	


•

HTTP API	


•

Shortcode API	


•

File Header API	


•

Theme Modification API	


•

Filesystem API	


•

Theme Customization API	


•

Metadata API	


•

Transients API	


•

Options API	


•

Widgets API	


•

Plugin API	


•

XML-RPC WordPress API	


•

Quicktags API

•

Image Manipulation API
Hooks & Filters
•

add_action( ‘hook_name’, ‘my_hook_callback’ );	


•

do_action( ‘hook_name’ );	

!

•

add_filter( ‘filter_name’, ‘my_filter_callback’ );	


•

$new_value = apply_filters( ‘filter_name’, $old_value );
JavaScript Hooks
•

JavaScript events	


•

$('body').bind(’event’, function(event, the_value) { });	


•

$('body').trigger(’event', [ value ]);
Backwards Compatibility
Backwards Compatibility

•

Don’t remove code, deprecate it	


•

Don’t change unit tests but add new unit tests to test your update
Code Standards
Naming Conventions
•

function some_name( $some_variable ) { [...] }	


•

class Walker_Category extends Walker { [...] } 	


•

class WP_HTTP { [...] }	


•

my-plugin-name.php	


•

class-my-class.php
Databases
•

Always use the WP API!	


•

$wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE `ID` = %d",
$id );
PHP standards
•

if ( contidion )	


•

my_function( $parameter )	


•

$array[‘index’]	


•

$array[ $index ]	


•

WordPress coding standards configuration for PhpStorm by Rarst:

https://gist.github.com/Rarst/1370155
Yoda Conditions
if ( true === $the_force ) { 

	

 $victorious = you_will( $be ); 

}	

!

A little bizarre, it is, to read. Get used to it, you will.
Code Templates
Code Templates

•

Predefined code with parameters	


•

https://gist.github.com/barrykooij/7632945
The project
Open source your code!
What is someone ‘steals’ my code?!
What is someone tells me it’s poorly written?!
Open source your project
•

Yes, it’s definitely scary.	


•

It’s what drives our community	


•

It’s the future, not only of code
Versioning

•

Work with major, minor and bugfix release (x.y.z)
Changelog

•

Always keep a public change log	


•

Give props to other people
WordPress.org Repository
WordPress repository
•

Use a header image	


•

Have clear description what your plugin does	


•

Non consumers rate by downloads combined with rating
Plugin Development @ WordCamp Norway 2014
Ask for ratings
•

Users don’t mind rating your plugin	


•

Don’t ask right away, let the user use your product first	


•

Make it a optional, and make this very clear!
Performance
Object Orientated Programming

•

Write OOP code, period.	


•

Don’t know how? Learn it!
Conditional loading of code
•

Don’t load every class on every page load	


•

Don’t initialise every class on every page load	


•

Split frontend and backend classes - is_admin()	


•

Use an autoloader
Documentation
Code documentation
•

Write code documentation, people will love you for it!	


•

Write inline documentation directly after the function declaration	

•
•

•

You won’t ‘forget’ writing it afterwards	

You’re making yourself rethink what you function should do	


All good IDE’s will use the inline documentation for autocompletion
Inline documentation

•

Explain what you’re doing	


•

Explain why you’re doing it
Security
Sanitizing data
•

Checking if data is of an expected structure (e.g. email)	

•

•

is_email	


Reformatting data so it will be of expected structure (e.g. title)	

•

sanitize_title	

http://codex.wordpress.org/Data_Validation#Input_Validation
Escaping data
•

Prevent XSS attacks, escape your data!	


•

esc_url	


•

esc_html
Capabilities
•

Is the user allowed to do this?	

!

// Check capabilities

if ( ! current_user_can( 'manage_options' ) ) {

	

 wp_die( __( 'Cheatin’ uh?' ) );

}
Direct file access
•

Don’t allow direct access to files	


•

Place this at the top of your files:	


if ( ! defined( 'ABSPATH' ) ) {

	

 header( 'HTTP/1.0 403 Forbidden' );

	

 die;

}
Asynchronous Javascript And XML
AJAX
AJAX allows you to run server side scripts
asynchronous
WordPress made AJAX really simple!
Register the AJAX action
•

Tell WordPress your plugin is accepting AJAX requests	


•

add_action( ‘wp_ajax_my_ajax_action’, array( $this, ’callback’ ) );	


•

add_action( ‘wp_ajax_nopriv_my_ajax_action’, array( $this, ’callback’ ) );
Use jQuery to do the POST
jQuery.post(

	

 ajaxurl,

	

 {

	

 	

 action 	

	

 : 'my_ajax_action',

	

 	

 ajax_nonce	

 : jQuery(‘.wpseo_redirects_ajax_nonce').val(),

	

 	

 my_var 	

 : my_val

	

 },

	

 function (response) {

	

 	

 // Do something

	

 }

);
Internationalisation
Make your plugin translatable
•

load_plugin_textdomain( ‘my-textdomain', false, dirname( plugin_basename(
__FILE__ ) ) . '/languages/' );	

!

•

__( ‘My String’, ‘my-textdomain’ );	


•

_e( ‘My String’, ‘my-textdomain’ );
Generate a .pot file
Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014
Unit Tests
An automated piece of code that invokes your
application code to check a single assumption.
Unit Test example
function test_get_single_role_by_user_query() { 	

	

	

	

	


// Arrange

$this->factory->user->create_many( 2, array( 

	

 'role' => 'subscriber' 

) ); 	


	

	

	


// Act

$wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); 

$users = $wp_user_search->get_results();


	

	

}

// Assert

$this->assertEquals( 2, count( $users ) );




Code
Loading your plugin

// Create object - Plugin init

add_action( 'plugins_loaded', create_function( '', 'new MyPlugin();' ) );
Plugin DIR & FILE
if ( ! defined( ’X_PLUGIN_DIR' ) ) {

	

 define( 'X_PLUGIN_DIR’, plugin_dir_path( __FILE__ ) );

}	

if ( ! defined( ’X_PLUGIN_FILE' ) ) {

	

 define( ’X_PLUGIN_FILE', __FILE__ );

}
Namespaces are great!
But, you can’t use them.
Prefixing
•

WordPress : PHP version 5.2.4 or greater	


•

Namespaces : 5.3.0 or greater	


•

Prefix your classes	


•

class WPSEO_Admin { [...] }
Test your plugin on PHP 5.2
Thank you.	

!

Find me on Twitter:

@CageNL

More Related Content

PDF
Capybara testing
PDF
Lunch and learn: Cucumber and Capybara
PDF
So, you want to be a plugin developer?
PPTX
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
PDF
Jumping Into WordPress Plugin Programming
PPTX
Nightwatch JS for End to End Tests
PDF
The LAZY Developer's Guide to BDD (with Cucumber)
PPTX
Test automation with Cucumber-JVM
Capybara testing
Lunch and learn: Cucumber and Capybara
So, you want to be a plugin developer?
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
Jumping Into WordPress Plugin Programming
Nightwatch JS for End to End Tests
The LAZY Developer's Guide to BDD (with Cucumber)
Test automation with Cucumber-JVM

What's hot (20)

PDF
Behavior Driven Web UI Automation with Selenium and Cucumber/SpecFlow (BDDx L...
PPTX
WordPress automation and CI
PDF
Tek 2013 - Building Web Apps from a New Angle with AngularJS
PDF
Behavior Driven Development with Cucumber
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
PPTX
jQuery Conference 2012 keynote
PPTX
Configuration as Code: The Job DSL Plugin
KEY
Sizzle jQCon San Francisco 2012
PPTX
Automated Testing with Cucumber, PhantomJS and Selenium
PDF
A Simple Plugin Architecture for Wicket
PPT
AngularJS for Legacy Apps
PDF
Automated Testing in WordPress, Really?!
PDF
The Onion
KEY
New Perspectives on Performance
PPTX
Creating a Plug-In Architecture
PDF
cf.Objective() 2017 - Design patterns - Brad Wood
PPT
WordPress and Ajax
PDF
Modern Web Application Development Workflow - EclipseCon France 2014
PDF
jQuery Chicago 2014 - Next-generation JavaScript Testing
PDF
Dependency Injection
Behavior Driven Web UI Automation with Selenium and Cucumber/SpecFlow (BDDx L...
WordPress automation and CI
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Behavior Driven Development with Cucumber
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
jQuery Conference 2012 keynote
Configuration as Code: The Job DSL Plugin
Sizzle jQCon San Francisco 2012
Automated Testing with Cucumber, PhantomJS and Selenium
A Simple Plugin Architecture for Wicket
AngularJS for Legacy Apps
Automated Testing in WordPress, Really?!
The Onion
New Perspectives on Performance
Creating a Plug-In Architecture
cf.Objective() 2017 - Design patterns - Brad Wood
WordPress and Ajax
Modern Web Application Development Workflow - EclipseCon France 2014
jQuery Chicago 2014 - Next-generation JavaScript Testing
Dependency Injection
Ad

Viewers also liked (7)

PDF
Unit Testing in WordPress
PPTX
Plugin development wpmeetup010
PDF
Plugin Development - WP Meetup Antwerp
PDF
Related Content
PDF
Customizing Your WooCommerce Store
PDF
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
PDF
We Will VAT You
Unit Testing in WordPress
Plugin development wpmeetup010
Plugin Development - WP Meetup Antwerp
Related Content
Customizing Your WooCommerce Store
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
We Will VAT You
Ad

Similar to Plugin Development @ WordCamp Norway 2014 (20)

PPTX
Introduction to Plugin Programming, WordCamp Miami 2011
PDF
WordPress plugin development
PDF
Best Practices for WordPress
PDF
Wordpress as a framework
PPTX
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
PDF
Best practices-wordpress-enterprise
PPTX
Best Practices for WordPress in Enterprise
PPTX
WordPress Plugin development
PPT
WordPress Harrisburg Meetup - Best Practices
PPTX
A peek into the world of WordPress plugin development
PDF
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
PPTX
Best Practices for Building WordPress Applications
ODP
Best practices in WordPress Development
PDF
Best Practices in Plugin Development (WordCamp Seattle)
KEY
How To Write a WordPress Plugin
PDF
Write Your First WordPress Plugin
PDF
Write your first WordPress plugin
PPTX
WordPress Plugin Development
PDF
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
PDF
Important Topics for wordPress Interview.pdf
Introduction to Plugin Programming, WordCamp Miami 2011
WordPress plugin development
Best Practices for WordPress
Wordpress as a framework
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
Best practices-wordpress-enterprise
Best Practices for WordPress in Enterprise
WordPress Plugin development
WordPress Harrisburg Meetup - Best Practices
A peek into the world of WordPress plugin development
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
Best Practices for Building WordPress Applications
Best practices in WordPress Development
Best Practices in Plugin Development (WordCamp Seattle)
How To Write a WordPress Plugin
Write Your First WordPress Plugin
Write your first WordPress plugin
WordPress Plugin Development
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
Important Topics for wordPress Interview.pdf

Recently uploaded (20)

PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PDF
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
PDF
Examining Bias in AI Generated News Content.pdf
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
substrate PowerPoint Presentation basic one
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
Introduction to MCP and A2A Protocols: Enabling Agent Communication
PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PDF
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
Altius execution marketplace concept.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PPTX
Build automations faster and more reliably with UiPath ScreenPlay
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
Decision Optimization - From Theory to Practice
SGT Report The Beast Plan and Cyberphysical Systems of Control
Data Virtualization in Action: Scaling APIs and Apps with FME
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
Examining Bias in AI Generated News Content.pdf
LMS bot: enhanced learning management systems for improved student learning e...
EIS-Webinar-Regulated-Industries-2025-08.pdf
substrate PowerPoint Presentation basic one
Advancing precision in air quality forecasting through machine learning integ...
Introduction to MCP and A2A Protocols: Enabling Agent Communication
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
4 layer Arch & Reference Arch of IoT.pdf
Altius execution marketplace concept.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Build automations faster and more reliably with UiPath ScreenPlay
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
Decision Optimization - From Theory to Practice

Plugin Development @ WordCamp Norway 2014