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 Slide

  2. View Slide

  3. Folder Structure
    Headers
    Events
    Plugin Development

    View Slide

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

    View Slide

  5. 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 Slide

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

    View Slide

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

    View Slide


  8. do_action( ‘div_content’ );
    ?>

    Events: Hooks - Example

    View Slide

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

    View Slide


  10. Hello world!

    Events: Hooks - Example

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. /*
    * 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 Slide

  18. 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 Slide

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

    View Slide