2017 © Pedro Peláez
 

library wp-hooks

Split Wordpress functions.php file into small, readable hooks

image

paperpixel/wp-hooks

Split Wordpress functions.php file into small, readable hooks

  • Wednesday, January 20, 2016
  • by paperpixel
  • Repository
  • 3 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

WPHooks

Split Wordpress functions.php file into small, readable hooks., (*1)

Requirement

Your Wordpress files and plugins should be managed by composer. See Bedrock boilerplate Wordpress structure to get started., (*2)

Installation

Install with Composer: composer require paperpixel/wphooks, (*3)

Usage

Creating a hook

Create a hook by extending WPHook class :, (*4)

use WPHooks\WPHook;

class ExampleHook extends WPHook {
    // Mandatory method, used to register actions and filters with Wordpress.
    public function register() {
        $this->add_action('init', 'my_action_hook');
        $this->add_filter('the_title', 'my_filter_hook');
    }

    private function my_action_hook() {
        // Stuff here will be called by Wordpress
    }

    private function my_filter_hook($title) {
        // Stuff here will be called by Wordpress
    }
}

Note: we take advantage of Composer autoload feature to import class by namespace with the keyword use., (*5)

Using the hook

In your functions.php class, instantiate ExampleHook and call register() method., (*6)

// functions.php
include 'ExampleHook.php';

$example_hook = new ExampleHook();
$example_hook->register();

Easier hook creation with WPHookLoader

You can use WPHookLoader to instantiate your hooks easily :, (*7)

// functions.php

include 'ExampleHook.php';
include 'Hook1.php';
include 'Hook2.php';
include 'Hook3.php';

// You can pass a WPHook instance
WPHooks\WPHookLoader::register(new ExampleHook());

// Or an array of WPHook instances
WPHooks\WPHookLoader::register([
   new Hook1(),
   new Hook2(),
   new Hook3(),
   ...
]);

Autoloading

We can leverage Composer's ClassLoader feature in our theme, so that we don't have to require each hook in functions.php., (*8)

Considering this theme structure :, (*9)

hooks/
    actions/
        ExampleAction.php
    filters/
        ExampleFilter.php
__autoload.php
functions.php

Start by creating the __autoload.php file :, (*10)

// __autoload.php

use Composer\Autoload\ClassLoader;

$loader = new ClassLoader();
$loader-> register();

$loader->addPsr4('Hooks\\Actions\\', __DIR__ . '/hooks/actions');
$loader->addPsr4('Hooks\\Filters\\', __DIR__ . '/hooks/filters');

In your Hook class, add a namespace accordingly to __autoload.php :, (*11)

// hooks/actions/ExampleAction.php

namespace Hooks\Actions;

use WPHooks\WPHook;

class ExampleAction extends WPHook {
    function register() {
        ...
    }
}

Finally, in functions.php, instantiate your hook :, (*12)

// functions.php

// Without WPHookLoader
$example_actions = new Actions\ExampleAction();

// With WPHookLoader
WPHooks\WPHookLoader::register(new Actions\ExampleAction());

The Versions

20/01 2016

dev-master

9999999-dev http://github.com/PaperpixelStudio/WPHooks

Split Wordpress functions.php file into small, readable hooks

  Sources   Download

BSD

The Requires

  • php >=5.3.9

 

wordpress

20/01 2016

0.4.0

0.4.0.0 http://github.com/PaperpixelStudio/WPHooks

Split Wordpress functions.php file into small, readable hooks

  Sources   Download

BSD

The Requires

  • php >=5.3.9

 

wordpress

20/01 2016

0.3.0

0.3.0.0 http://github.com/PaperpixelStudio/WPModules

Split Wordpress functions.php file into small, readable modules

  Sources   Download

BSD

The Requires

  • php >=5.3.9

 

wordpress

20/01 2016

0.2.0

0.2.0.0 http://github.com/PaperpixelStudio/WPModules

Split Wordpress functions.php file into small, readable modules

  Sources   Download

BSD

The Requires

  • php >=5.3.9

 

wordpress

20/01 2016

0.1.0

0.1.0.0 http://github.com/PaperpixelStudio/WPModules

Split Wordpress functions.php file into small, readable modules

  Sources   Download

BSD

The Requires

  • php >=5.3.9

 

wordpress