The Backyard framework comes with an helper
Redirects class to handle redirects and flash admin notices.
Add the service provider
From your plugin's entry file, use the
addServiceProvider() method of the
Plugin class to register the redirect service provider.
$myPlugin->addServiceProvider( Backyard\Redirects\RedirectServiceProvider::class );
Once the provider has been registered, the
Plugin container has access to a new
redirect() method which returns a pre-configured instance of the
Redirect to an url
$myPlugin->redirect()->toUrl( admin_url() )->now();
When creating a redirect, use the
toUrl() method to set the url to redirect to.
When executing the redirect, use the
Redirect with an admin notice
To flash a notice in the admin panel, you need to chain the redirect with the
withNotice method. The function takes 2 arguments. The
$message of the notice and
$type of notice. Types can be success, warning, danger, info.
// Success message $myPlugin->redirect() ->toUrl( admin_url() ) ->withNotice( 'Success message here' ) ->now(); // Warning message $myPlugin->redirect() ->toUrl( admin_url() ) ->withNotice( 'Warning message here', 'warning' ) ->now();
Redirect to the previous page
To redirect to the previous page, you may use the