承接 paperpixel/wp-hooks 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

paperpixel/wp-hooks

Composer 安装命令:

composer require paperpixel/wp-hooks

包简介

Split Wordpress functions.php file into small, readable hooks

关键字:

README 文档

README

Split Wordpress functions.php file into small, readable hooks.

Requirement

Your Wordpress files and plugins should be managed by composer. See Bedrock boilerplate Wordpress structure to get started.

Installation

Install with Composer: composer require paperpixel/wphooks

Usage

Creating a hook

Create a hook by extending WPHook class :

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.

Using the hook

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

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

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

Considering this theme structure :

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

Start by creating the __autoload.php file :

// __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 :

// hooks/actions/ExampleAction.php

namespace Hooks\Actions;

use WPHooks\WPHook;

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

Finally, in functions.php, instantiate your hook :

// functions.php

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

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

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2016-01-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固