The WordPress filter, action system in Laravel

Overview

Laravel Hooks

The WordPress filters, actions system in Laravel.

About

  • An action interrupts the code flow to do something, and then returns back to the normal flow without modifying anything.
  • A filter is used to modify something in a specific way so that the modification is then used by code later on.

Read more about filters and actions

Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots.

Installation

composer require millat/laravel-hooks

DONE! Now you can use laravel-hooks

Functions

do_action(  string $tag,  mixed $arg )

This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the action to be executed.

  • $arg (mixed) (Optional) Additional arguments which are passed on to the functions hooked to the action. Default empty.

add_action( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Actions are the hooks that the Laravel application launches at specific points during execution, or when specific events occur.

Parameters

  • $tag (string) (Required) The name of the action to add the callback to.

  • $callback (callable) (Required) The callback to be run when the action is called.

  • $priority (int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

apply_filters( string $tag, mixed $value )

This function invokes all functions attached to filter hook $tag. It is possible to create new filter hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the filter hook.

  • $value (mixed) (Required) The value to filter.

add_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Filter hooks to allow modify various types of internal data at runtime.

Parameters

  • $tag (string) (Required) The name of the filter to add the callback to.

  • $callback (callable) (Required) The callback to be run when the filter is applied.

  • $priority (int) (Optional) Used to specify the order in which the functions associated with a particular filter are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the filter. Default value: 10

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

How to pass callback function?

The callback function could be a string referring to a class in the application MyNamespace\Http\Listener@myHookListener, an array callback [$object, 'method'] or a globally registered function global_function, anonymous function.

Example using anonymous function:

add_action('user_created', function($user) {
    $user->sendWelcomeMail();
}, 20, 1);

Example using referring to a class's method:

add_action('user_created', 'MyNamespace\Http\MyClass@myMethod', 20, 1);

Example using array callback:

add_action('user_created', [$object, 'myMethod'], 20, 1);

Usage

Actions

Wherever you want, you can create a new action in you Laravel application:

do_action('user_created', $user);

Here, user_created is the name of the action, which will use later when the action will be listening. And $user is the parameters, which will be found whenever you listen the action. These can be anything.

To listen to your actions, you attach listeners. These are best added to your AppServiceProvider boot() method.

For example if you wanted to hook in to the above hook, you could do:

add_action('user_created', function($user) {
    $user->sendWelcomeMail();
}, 20, 1);

The first argument must be the name of the action. The second would be a closures, callbacks and anonymous functions. The third specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. Default value: 10. And fourth, the number of arguments the function accepts. Default value: 1

Filters

Filters always have to have data coming in and data going out to ensure the data is output in the browser (your content may be passed through other filters before getting output in the browser). By comparison, actions, which are similar to filters, do not require that anything to be returned, although data can be returned through actions as well.

Basically, filters are functions that can be used in Laravel application to pass data through. They allows developers to modify the default behavior of a specific function.

Here's an example of how filter used in a real application.

Post.php is a model or class , where it builds a query to fetch all published posts

class Post extend Model
{
    public function getPublished()
    {
        return Post::where('published_at', '>', now());
    }
}

Using filter we can make a offer to modify this query:

class Post extend Model
{
    public function getPublished()
    {
        return apply_filters('posts_published', Post::where('published_at', '>', now());
    }
}

Now, in the entry point of application like any module or plugin's you can modify this post published query.

In Module's or Plugin's service provider (preferably in the boot method) we'll add a listener for the filter.

class ModuleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        add_filter('posts_published', function($query) {
            return $query->where('status', 'active');
        });
    }
}

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Integrating Laravel into WordPress

WordPress Laravel Bootstrap A WordPress plugin helps you use functions, methods, libraries of Laravel in any WordPress projects Requiments Laravel =

Wordpress integrated with Laravel via Composer. Together, but independents.
Wordpress integrated with Laravel via Composer. Together, but independents.

Wordpress integrated with Laravel via Composer. Atention! The branch master is no longer manteined. Now I'm working on branch light. Not booting Larav

Seamlessly integrate Wordpress with Laravel.
Seamlessly integrate Wordpress with Laravel.

Documentation What is this? This is your standard Laravel install but with a few little extras 😉 . Koselig is your gateway to Wordpress from Laravel.

Use WordPress backend with Laravel or any PHP application
Use WordPress backend with Laravel or any PHP application

A collection of Model classes that allows you to get data directly from a WordPress database. Corcel is a collection of PHP classes built on top of El

Brings Laravel's great template engine, Blade, to WordPress

###This plugin is deprecated in favor of ekandreas/bladerunner WordPress Blade Brings Laravel's great template engine, Blade, to WordPress. Just insta

The WordPress theme powered by the Laravel Framework.

Laravel in WordPress Theme Laravel is a web application framework with expressive, elegant syntax. It's one of the most popular PHP frameworks today.

Use WordPress backend with Laravel or any PHP application
Use WordPress backend with Laravel or any PHP application

A collection of Model classes that allows you to get data directly from a WordPress database. Corcel is a collection of PHP classes built on top of El

WordPress Framework based on parent theme

Cherry Framework The most delicious WordPress framework Fully responsive design, easy install, steady updates, great number of shortcodes and widgets,

Releases(1.2.1)
Owner
Md Ahsanul Haque Millat
Qualified PHP developer with experience in the layout, design and coding of websites, framework, CMS specifically in PHP format.
Md Ahsanul Haque Millat
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.

WP Bootstrap Navwalker This code in the main repo branch is undergoing a big shakeup to bring it in line with recent standards and to merge and test t

WP Bootstrap 3.3k Jan 5, 2023
A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities.

Awesome WordPress A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities. Inspired by bayand

Dropndot Limited 91 Dec 26, 2022
The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone.

Pronamic WordPress Basecone The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone. Table of contents Au

Pronamic 1 Oct 19, 2021
A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings.

Suspend WP A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings. ?? Demo (coming so

Waren Gonzaga 3 Nov 15, 2021
Twenty Twenty-Two, the default WordPress theme that will launch with WordPress 5.9.

Twenty Twenty-Two Welcome to the development repository for the default theme that will launch with WordPress 5.9. About Twenty Twenty-Two is designed

null 414 Nov 28, 2022
Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer.

Description Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer. Support [audio] tag, compatible with AMP. Requirement WordPr

Karl Chen 24 Nov 3, 2022
WordPress plugin that lets you use Discourse as the community engine for a WordPress blog

WP Discourse Note: the wp-discourse plugin requires >= PHP-5.4.0. The WP Discourse plugin acts as an interface between your WordPress site and your Di

Discourse 497 Dec 10, 2022
WordPress & TypeScript. Simple starter template for WordPress projects

WordPress & TypeScript. Simple starter template for WordPress projects that want to use TypeScript in combination with @wordpress/scripts

Make it WorkPress 11 Sep 27, 2022
A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API

Wordpress XML-RPC PHP Client A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API Created by Hieu Le MIT licensed. Cur

Hieu Le 112 Nov 10, 2022
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.

Laravel Blog Have you worked with Wordpress? Developers call this package wordpress-like laravel blog. Contact us for any customization: contact@binsh

Binshops 279 Dec 28, 2022