plugin development - wp meetup antwerp

49
Plugin Development Antwerp WordPress Meetup

Upload: barry-kooij

Post on 14-Jul-2015

224 views

Category:

Software


0 download

TRANSCRIPT

Plugin DevelopmentAntwerp WordPress Meetup

Hallo, met Barry

WooCommerce Developer @ WooThemes

Related Posts for WordPressDownload MonitorPost ConnectorWhat The File

My Setup

MacBook Pro Retina

PHPStorm

GIT : CLI & SourceTree

Vagrant, Precise32 box, custom provision script

Coding Standards

Naming Conventions

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

class Walker_Category extends Walker { [...] }

class WP_HTTP { [...] }

my-plugin-name.php

class-my-class.php

PHP standards

if ( condition )

my_function( $parameter )

$array['index']

$array[ $index ]

PHPStorm 8+ supports WordPress code standards : ALT + CMD + L

Yoda Conditions

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

!

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

Databases

Always use the WP API!

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

Tips

Debugging

WP_DEBUG

Xdebug

Backwards Compatibility

Deprecate instead/before removing

Code Templates

Basic plugin in a minute

https://gist.github.com/barrykooij/abe272917d536fb0f777

The Project

Open Source ?

What if someone steals my code?!

What if someone says my code sucks?!

Open Source!

Yes, it’s definitely scary at first

It’s what drives our community

It’s the future, not only for code

Versioning

Work with major, minor and bug-fix releases (x.y.z)

Changelog

Always keep a public changelog

Always include dates in your changelog

Give props to those who deserve it

WordPress.org

Repository

WP Repository

Use a header image and plugin logo

Have a clear description of what your plugin does

People rate by downloads, ratings AND active installs(!)

Image Specs

Header Imagebanner-772x250.jpg

Iconicon-256x256.png & icon-128x128.png

Performance

Write with performance in mind

Bad performance is a deal breaker

Object Orientated Programming

Write OO code, period.

Don’t know how? Learn it. Believe me, it’s worth it.

Documentation

Code Documentation

Write code documentation, people will love you for it!

Write code documentation directly after the method declaration

You won’t ‘forget’ writing it afterwards

You’re making yourself rethink what you function should do

Good IDE’s will use the documentation for autocompletion

Inline Documentation

Explain what you’re doing

Explain why you’re doing it

Site Documentation

Simple pages

Custom Post Type

HelpScout Knowledge Base

Security

Sanitizing Data

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

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

codex.wordpress.org/Data_Validation#Input_Validation

Escaping Data

XSS attacks

esc_url

esc_html

Capabilities

User X allowed to do Y?

if ( ! current_user_can( 'manage_options' ) ) {

Direct File Access

Prevent direct file access

ABSPATH check

if ( ! defined( 'ABSPATH' ) ) {

header( 'HTTP/1.0 403 Forbidden' );

die();

}

Internationalization

i18n

Translatable Plugins

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

!

__( 'My String', 'my-textdomain' );

_e( 'My String', 'my-textdomain' );

Translation Files

Generate a .pot file

GlotPress

Transifex

Code

Hooks

add_action( ‘hook_name’, ‘my_hook_callback’ );

do_action( ‘hook_name’ );

Filters

add_filter( 'filter_name', 'my_filter_callback' );

$new_value = apply_filters( 'filter_name', $old_value );

JavaScript events

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

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

Plugin FILE

// Define define( 'RP4WP_PLUGIN_FILE', __FILE__ );

// Load main plugin file require_once plugin_dir_path( RP4WP_PLUGIN_FILE ) . 'classes/class-rp4wp.php';

Prefix Classes & Methods

WordPress : PHP version 5.2.4 or greater

Namespaces : 5.3.0 or greater

Prefix your classes

class RP4WP_Related_Word_Manager

Test your plugin on

PHP 5.2.4

Barry Kooij

@CageNL

barrykooij.com