Skip to content
Frank Corso edited this page Aug 6, 2020 · 2 revisions

Using Popup Maker's alert system, you can quickly add new alerts for admins that can be dismissed and can include actions for the admin to take.

Adding the alert

The main component of the alert is the array you need to append to the alert list which is filtered using the pum_alert_list filter.

add_filter( 'pum_alert_list', 'my_new_alert_function' );

Within your function, append an array to the passed array. Refer to our alert system docs for reference on what the keys do and which values they accept.

Be sure to only enqueue the alert when it is needed. For example, if you have a flag or option that designates an error has occurred, check for that within your alerts function.

/**
* Adds our new alert
*
* @param array The registered alerts.
* @return array The registered alerts with our alert added.
*/
function my_new_alert_function( $alerts ) {
    if ( true == get_option( 'error_occurred', false ) ) { // Have your check for if you need to show the alert. If not, return $alerts early.
        return $alerts;
    }
    $alerts[] = array(
        'code'        => 'example_alert', // Unique ID for the alert.
        'type'        => 'info', // Type of alert. Controls styling of the alert.
        'dismissible' => true, // Is the alert dismissible?
        'global'      => false, // Should the alert be displayed in all admin areas or only Popup Maker admin areas?
        'priority'    => 10, // Where in the list of alerts should it go?
        'message'     => "Example alert's message.", // Message within the alert.
        'actions'     => array( // The possible actions for the admin to take. Optional.
            array(
                'primary' => true, // Primary actions get the highlighted link/button.
                'type'    => 'action', // action or link.
                'action'  => 'example_alert_allow', // Unique ID of the action or 'dismiss'.
                'text'    => 'Allow', // Text for the button or link.
            ),
            array(
                'primary' => false,
                'type'    => 'action',
                'action'  => 'example_alert_not_allow',
                'text'    => 'Do not allow',
            ),
            array(
                'primary' => false,
                'type'    => 'link',
                'action'  => '',
                'text'    => 'Learn more',
                'href'    => 'https://google.com',
            ),
        ),
    );
    return $alerts;
}

Handling actions taken

If your alert allows for the admin to take an action that is not 'dismiss', these will be passed to the pum_alert_dismissed hook.

add_action( 'pum_alert_dismissed', 'my_example_alert_handler', 10, 2 );

This hook will pass two variables, the code which is the ID of the alert and the action which is the ID for the action the user took.

/**
* Handles any action taken by the user
*
* @param string $code The alert.
* @param string $action The action.
*/
function my_example_alert_handler( $code, $action ) {
    if ( 'example_alert' === $code ) { // Check to make sure the alert an action was taken on was your alert.
        switch ( $action ) {
            case 'example_alert_allow':
                // Code for when the user clicked the 'Allow' action.
                break;

            case 'example_alert_not_allow':
                // Code for when the user clicked the 'Do not allow' action.
                break;

            default:
                // Any code needed for when the user clicks a different action or your catch-all for the default actions.
        }
    }
}