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

Bending WordPress to Your Will (October 2018)

Bending WordPress to Your Will (October 2018)

These slides are slightly modified from a previous talk I gave with the same title.

WordPress is an incredibly powerful platform and framework for development, but until you learn how to use it you won't be able to make full use of its flexibility. In this session you will learn the core principles of WordPress development that will enable you to start taking full advantage of everything that it offers.

I gave this presentation at the Cape Town Develop UG on 29 October 2018: https://www.meetup.com/DeveloperUG/events/bcqgbqyxnbdc/

Hugh Lashbrooke

October 29, 2018
Tweet

More Decks by Hugh Lashbrooke

Other Decks in Programming

Transcript

  1. Bending WordPress
    To Your Will

    View full-size slide

  2. Folder Structure
    Headers
    Events
    Plugin Development

    View full-size slide

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

    View full-size 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: https://hugh.blog/
    * Requires at least: 4.6
    * Tested up to: 4.9.8
    *
    * Text Domain: seriously-simple-podcasting
    */

    View full-size slide

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

    View full-size slide

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

    View full-size slide


  7. do_action( ‘div_content’ );
    ?>

    Events: Hooks - Example

    View full-size slide

  8. add_action( ‘div_content’, ‘my_content’ );
    function my_content() {
    echo “Hello world!”;
    }
    Events: Hooks - Example

    View full-size slide


  9. Hello world!

    Events: Hooks - Example

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. $v = apply_filters( 'body_class', $classes );
    Events: Filters - Example

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  16. /*
    * Plugin Name: YouTube Redirect
    * Version: 1.0
    */
    add_action( ‘init’, ‘youtube_redirect’ );
    function youtube_redirect () {
    if( isset( $_GET[‘youtube_redirect’] ) ) {
    wp_redirect( ‘https://youtube.com’, 302 );
    exit;
    }
    }
    ?>
    Bringing it Together

    View full-size slide

  17. Varying Vagrant Vagrants:
    varyingvagrantvagrants.org
    Plugin Developer Info:
    wordpress.org/plugins/developers
    Coding Standards:
    make.wordpress.org/core/handbook/best-practices/coding-standards
    Plugin Template:
    github.com/hlashbrooke/WordPress-Plugin-Template

    View full-size slide

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

    View full-size slide