HTTPS SSH

Sewn In Notification Box

A plugin for Wordpress that creates a very pluggable framework to add notifications on the front end of your application. Notifications can be generated by query variables or through template functions and actions.

This plugin can be used to create sitewide notifications, or you can narrow it down to specific pages.

Add the Notification Center to Your Template(s)

The most basic way to add notifications is sitewide. You can add the "show" action to a template like your header.php file:

<?php do_action( 'sewn_notifications/show' ); ?>

Create Query Variable Notifications

The simplest way to extend the notification center is to add query variable key/value pairs to generate a new message.

<?php
add_filter( 'sewn_notifications/queries', 'custom_add_query_notifications' );
    function custom_add_query_notifications( $queries )
    {
        $queries[] = array(
            'key' => 'update',
            'value' => 'true',
            'message' => "Updated.",
            'args' => 'fade=true'
        );
        $queries[] = array(
            'key' => 'update',
            'value' => 'failed',
            'message' => "Unable to update.",
            'args' => 'dismiss=true&error=true'
        );
        $queries[] = array(
            'key' => 'update',
            'value' => 'finished',
            'message' => "Finished!",
            'args' => 'fade=10000' // wait ten seconds before fading
        );
        return $queries;
    }
?>

Now whenever a query variable is added to the end of a page, a message can be shown in the notification center.

So http://example.com/path/to/page/?update=true will generate the message: "Updated." in the notification center.

Arguments for Messages

When adding a message, there are arguments you can use to customize the functionality.

  1. error When set to true, this will add the error class to the message. Default: false
  2. fade When set to true, this will fade the message out after 3 seconds. You can change the pause time by setting fade to a number (in milliseconds). Default: false
  3. dismiss Allows a message to be dismissed by the user with a close button. Default: false
  4. event Allows message to be persistent, so that it shows up on each page load unless dismissed. The event tracks the message and updates the user's meta when the message is dismissed, so it will stop showing up. Default: false
  5. page Lock a message to a specific page only. When page is set, a is_page($page) check will be performed before showing the message. Particularly useful when setting up query variable pairs. Default: false

Maually Generate Notifications

By default notifications are static and global, so you can add new notifications using the class methods:

if ( $something_happened ) :
    sewn_notifications::add_notification( $message, $args );
endif;

When used before the "show" action will add a message to the notificaiton center. Or you can manually show notifications using:

sewn_notifications::show_notifications();

If needed, you can also generate multiple notification streams by instantiating the class.

$notifications_1 = new sewn_notifications();
$notifications_1->add_notification( "Hello world." );
$notifications_1->show_notifications();

$notifications_2 = new sewn_notifications();
$notifications_2->add_notification( "Foo bar." );
$notifications_2->show_notifications();

This would generate two notification centers.

Note the "show" action will show ALL messages from all instances. The query filter above will also act globally, but the method can be accessed by a specific instance as well: $notifications_1->add_query() to add each query needed.