Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.44 KB

actions.md

File metadata and controls

46 lines (34 loc) · 1.44 KB

Actions

Using Actions in Service Providers

Easily hook into WordPress' actions.

Instead of creating a function then calling that function inside of WordPress' add_action function, simply access it through the service container and use a callback.

Let's say we've created a service provider specifically for theme functions. We'll send an email on save_post.

<?php namespace App\Providers;

use Illuminate\Mail\Message;
use Illuminate\Support\ServiceProvider;

class ThemeFunctionsServiceProvider extends ServiceProvider
{

    public function register()
    {
        $this->app['actions']->listen('save_post', function($id, $post) {
            app('mailer')->send('emails.saved_post', $post, function(Message $message) {
                $message->from('no-reply@example.com');
                $message->to('admin@example.com');
                $message->subject('Post was saved');
            });
        });
    }
}

Using Actions with the App Helper Function

Of course you can also hook into WordPress actions using the app helper function.

app('actions')->listen('save_post', function() {
    //Code goes here
});