70
Plugin Development WordCamp Norway 2014

Plugin Development @ WordCamp Norway 2014

Embed Size (px)

DESCRIPTION

The Plugin Development presentation I gave at WordCamp Norway 2014

Citation preview

Page 1: Plugin Development @ WordCamp Norway 2014

Plugin DevelopmentWordCamp Norway 2014

Page 2: 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

Page 3: Plugin Development @ WordCamp Norway 2014

Agenda

• Presentation

• Q&A

• Personal Q&A

Page 4: Plugin Development @ WordCamp Norway 2014

Let’s get coding!

Page 5: Plugin Development @ WordCamp Norway 2014

Hold up!

Page 6: Plugin Development @ WordCamp Norway 2014

There more to a good plugin than good code!

Page 7: Plugin Development @ WordCamp Norway 2014

Wait, what?

Page 8: Plugin Development @ WordCamp Norway 2014

My Setup

Page 9: Plugin Development @ WordCamp Norway 2014

My Setup

• MacBook Pro Retina

• PhpStorm

• GIT -> GitHub

• Vagrant w/ Varying Vagrant Vagrants (VVV) -10up

Page 10: Plugin Development @ WordCamp Norway 2014

Debugging

Page 11: Plugin Development @ WordCamp Norway 2014

Debuggingdefine( '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

Page 12: Plugin Development @ WordCamp Norway 2014

WordPress Core

Page 13: Plugin Development @ WordCamp Norway 2014

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

Page 14: Plugin Development @ WordCamp Norway 2014

API• Dashboard Widgets API

• Database API

• HTTP API

• File Header API

• Filesystem API

• Metadata API

• Options API

• Plugin API

• Quicktags API

• Rewrite API

• Settings API

• Shortcode API

• Theme Modification API

• Theme Customization API

• Transients API

• Widgets API

• XML-RPC WordPress API

• Image Manipulation API

Page 15: Plugin Development @ WordCamp Norway 2014

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 );

Page 16: Plugin Development @ WordCamp Norway 2014

JavaScript Hooks

• JavaScript events

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

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

Page 17: Plugin Development @ WordCamp Norway 2014

Backwards Compatibility

Page 18: Plugin Development @ WordCamp Norway 2014

Backwards Compatibility

• Don’t remove code, deprecate it

• Don’t change unit tests but add new unit tests to test your update

Page 19: Plugin Development @ WordCamp Norway 2014

Code Standards

Page 20: Plugin Development @ WordCamp Norway 2014

Naming Conventions

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

• class Walker_Category extends Walker { [...] }

• class WP_HTTP { [...] }

• my-plugin-name.php

• class-my-class.php

Page 21: Plugin Development @ WordCamp Norway 2014

Databases

• Always use the WP API!

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

Page 22: Plugin Development @ WordCamp Norway 2014

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

Page 23: Plugin Development @ WordCamp Norway 2014

Yoda Conditions

if ( true === $the_force ) { $victorious = you_will( $be ); }

!

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

Page 24: Plugin Development @ WordCamp Norway 2014

Code Templates

Page 25: Plugin Development @ WordCamp Norway 2014

Code Templates

• Predefined code with parameters

• https://gist.github.com/barrykooij/7632945

Page 26: Plugin Development @ WordCamp Norway 2014

The project

Page 27: Plugin Development @ WordCamp Norway 2014

Open source your code!

Page 28: Plugin Development @ WordCamp Norway 2014

What is someone ‘steals’ my code?!

Page 29: Plugin Development @ WordCamp Norway 2014

What is someone tells me it’s poorly written?!

Page 30: Plugin Development @ WordCamp Norway 2014

Open source your project

• Yes, it’s definitely scary.

• It’s what drives our community

• It’s the future, not only of code

Page 31: Plugin Development @ WordCamp Norway 2014

Versioning

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

Page 32: Plugin Development @ WordCamp Norway 2014

Changelog

• Always keep a public change log

• Give props to other people

Page 33: Plugin Development @ WordCamp Norway 2014

WordPress.org Repository

Page 34: Plugin Development @ WordCamp Norway 2014

WordPress repository

• Use a header image

• Have clear description what your plugin does

• Non consumers rate by downloads combined with rating

Page 35: Plugin Development @ WordCamp Norway 2014
Page 36: 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!

Page 37: Plugin Development @ WordCamp Norway 2014

Performance

Page 38: Plugin Development @ WordCamp Norway 2014

Object Orientated Programming

• Write OOP code, period.

• Don’t know how? Learn it!

Page 39: Plugin Development @ WordCamp Norway 2014

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

Page 40: Plugin Development @ WordCamp Norway 2014

Documentation

Page 41: Plugin Development @ WordCamp Norway 2014

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

Page 42: Plugin Development @ WordCamp Norway 2014

Inline documentation

• Explain what you’re doing

• Explain why you’re doing it

Page 43: Plugin Development @ WordCamp Norway 2014

Security

Page 44: Plugin Development @ WordCamp Norway 2014

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

Page 45: Plugin Development @ WordCamp Norway 2014

Escaping data

• Prevent XSS attacks, escape your data!

• esc_url

• esc_html

Page 46: Plugin Development @ WordCamp Norway 2014

Capabilities

• Is the user allowed to do this?

!

// Check capabilitiesif ( ! current_user_can( 'manage_options' ) ) { wp_die( __( 'Cheatin’ uh?' ) ); }

Page 47: Plugin Development @ WordCamp Norway 2014

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; }

Page 48: Plugin Development @ WordCamp Norway 2014

Asynchronous Javascript And XML

Page 49: Plugin Development @ WordCamp Norway 2014

AJAX

Page 50: Plugin Development @ WordCamp Norway 2014

AJAX allows you to run server side scripts asynchronous

Page 51: Plugin Development @ WordCamp Norway 2014

WordPress made AJAX really simple!

Page 52: Plugin Development @ WordCamp Norway 2014

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’ ) );

Page 53: Plugin Development @ WordCamp Norway 2014

Use jQuery to do the POSTjQuery.post( ajaxurl, { action : 'my_ajax_action', ajax_nonce : jQuery(‘.wpseo_redirects_ajax_nonce').val(), my_var : my_val }, function (response) { // Do something } );

Page 54: Plugin Development @ WordCamp Norway 2014

Internationalisation

Page 55: Plugin Development @ WordCamp Norway 2014

Make your plugin translatable

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

!

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

• _e( ‘My String’, ‘my-textdomain’ );

Page 56: Plugin Development @ WordCamp Norway 2014

Generate a .pot file

Page 57: Plugin Development @ WordCamp Norway 2014
Page 58: Plugin Development @ WordCamp Norway 2014
Page 59: Plugin Development @ WordCamp Norway 2014
Page 60: Plugin Development @ WordCamp Norway 2014

Unit Tests

Page 61: Plugin Development @ WordCamp Norway 2014

An automated piece of code that invokes your application code to check a single assumption.

Page 62: Plugin Development @ WordCamp Norway 2014

Unit Test examplefunction 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 ) ); }

Page 63: Plugin Development @ WordCamp Norway 2014

Code

Page 64: Plugin Development @ WordCamp Norway 2014

Loading your plugin

// Create object - Plugin init add_action( 'plugins_loaded', create_function( '', 'new MyPlugin();' ) );

Page 65: Plugin Development @ WordCamp Norway 2014

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__ ); }

Page 66: Plugin Development @ WordCamp Norway 2014

Namespaces are great!

Page 67: Plugin Development @ WordCamp Norway 2014

But, you can’t use them.

Page 68: Plugin Development @ WordCamp Norway 2014

Prefixing

• WordPress : PHP version 5.2.4 or greater

• Namespaces : 5.3.0 or greater

• Prefix your classes

• class WPSEO_Admin { [...] }

Page 69: Plugin Development @ WordCamp Norway 2014

Test your plugin on PHP 5.2

Page 70: Plugin Development @ WordCamp Norway 2014

Thank you. !

Find me on Twitter: @CageNL