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

Bending WordPress to your Will

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.

Hugh Lashbrooke

September 27, 2017
Tweet

More Decks by Hugh Lashbrooke

Other Decks in Technology

Transcript

  1. Bending WordPress
    To Your Will

    View Slide

  2. Folder Structure
    Headers
    Events
    Plugin Development

    View Slide

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

    View Slide

  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/
    */

    View Slide

  5. PHP
    call_user_func()
    WordPress
    do_action()
    apply_filters()
    Events

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. add_action( ‘hook’, ‘my_function’, 10, 2 );
    function my_function ( $foo, $bar ) {
    ...
    }
    Events: Parameters

    View Slide

  10. apply_filters( ‘filter’, $foo );
    add_filter( ‘filter’, ‘my_function’ );
    Filters must always return a value
    Events: Filters

    View Slide

  11. add_filter( ‘body_class’, ‘my_function’ );
    function my_function( $classes ) {
    $classes[] = ‘my-class’;
    return $classes;
    }
    Events: Filters

    View Slide

  12. remove_action( ‘hook’, ‘function’ );
    remove_filter( ‘filter’, ‘function’ );
    Priorities must match original hook/filter
    Removing Hooks & Filters

    View Slide

  13. /*
    * 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

    View Slide

  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

    View Slide

  15. https://github.com/hlashbrooke/Post-View-Counter
    Demo Plugin

    View Slide

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

    View Slide