plugin development - wp meetup antwerp
TRANSCRIPT
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
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 );
Code Templates
Basic plugin in a minute
https://gist.github.com/barrykooij/abe272917d536fb0f777
Open Source!
Yes, it’s definitely scary at first
It’s what drives our community
It’s the future, not only for code
Changelog
Always keep a public changelog
Always include dates in your changelog
Give props to those who deserve it
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(!)
Object Orientated Programming
Write OO code, period.
Don’t know how? Learn it. Believe me, it’s worth it.
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
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
Translatable Plugins
load_plugin_textdomain( 'my-textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
!
__( 'My String', 'my-textdomain' );
_e( 'My String', 'my-textdomain' );
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