Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bending WordPress to your Will

51eec74fdf430271ce134e58b811e5b9?s=47 Hugh Lashbrooke
September 27, 2017

Bending WordPress to your Will

This is a workshop that I presented at PHP South Africa 2017 - these slides were just the intro and the rest of the workshop was building a basic WordPress plugin.

51eec74fdf430271ce134e58b811e5b9?s=128

Hugh Lashbrooke

September 27, 2017
Tweet

More Decks by Hugh Lashbrooke

Other Decks in Technology

Transcript

  1. Bending WordPress To Your Will

  2. Folder Structure Headers Events Plugin Development

  3. wp-content/plugins/plugin-slug plugin-slug.php readme.txt Folder Structure

  4. Headers /* * Plugin Name: Seriously Simple Podcasting * Version:

    1.9.6 * Plugin URI: https://wordpress.org/plugins/seriously-simple-podcasting/ * Description: Podcasting the way it's meant to be. * Author: Hugh Lashbrooke * Author URI: http://www.hughlashbrooke.com/ * Requires at least: 4.0 * Tested up to: 4.2 * * Text Domain: seriously-simple-podcasting * Domain Path: /lang/ */
  5. PHP call_user_func() WordPress do_action() apply_filters() Events

  6. do_action( ‘hook’ ); add_action( ‘hook’, ‘my_function’ ); Events: Hooks

  7. add_action( ‘init’, ‘init_function’ ); function init_function() { ... } Events:

    Hooks
  8. add_action( ‘hook’, ‘my_function’, 10 ); Lower number == higher priority

    Events: Priorities
  9. add_action( ‘hook’, ‘my_function’, 10, 2 ); function my_function ( $foo,

    $bar ) { ... } Events: Parameters
  10. apply_filters( ‘filter’, $foo ); add_filter( ‘filter’, ‘my_function’ ); Filters must

    always return a value Events: Filters
  11. add_filter( ‘body_class’, ‘my_function’ ); function my_function( $classes ) { $classes[]

    = ‘my-class’; return $classes; } Events: Filters
  12. remove_action( ‘hook’, ‘function’ ); remove_filter( ‘filter’, ‘function’ ); Priorities must

    match original hook/filter Removing Hooks & Filters
  13. <?php /* * Plugin Name: Google Redirect * Version: 1.0

    */ add_action( ‘init’, ‘google_redirect’ ); function google_redirect () { if( isset( $_GET[‘google_redirect’] ) ) { wp_redirect( ‘http://www.google.com’, 302 ); exit; } } ?> Bringing it Together
  14. Plugin Developer Info: wordpress.org/plugins/developers/ Coding Standards: codex.wordpress.org/WordPress_Coding_Standards Action Reference: codex.wordpress.org/Plugin_API/Action_Reference

    Plugin Template: github.com/hlashbrooke/WordPress-Plugin-Template Helpful Links
  15. https://github.com/hlashbrooke/Post-View-Counter Demo Plugin

  16. automattic.com make.wordpress.org hugh.blog @hlashbrooke