Give 1.8.9 Give_Notices Class Update

In Give version 1.8.9+ all core notices (frontend + backend) will be generated with the Give_Notices class. We’ve put a lot of work in refactoring this class to handle much more than just output. Now you can specify the type of notice, whether it’s dismissible, and of course customize the content.

Our hope is that this update frees you from having to spend time writing custom notice handlers. As well, it should give a little speed boost to your sites because all JS is included conditionally based on whether a dismissible notice appears or not. Let’s explore further:

The Types of Notices

  1. Auto dismissible notices: These type of notices will automatically hide after 5 seconds. These are useful when updating settings and showing a “Settings updated” notice or similar.
  2. Manually dismissible notices: These type of notices will be hide for specific time interval (24 hours, 1 week, etc) or permanently per user or for all users. These are useful for reminders or
    1. Quick (24 hrs.) dismissed notice.
    2. Permanently dismissed notice
    3. Custom (time interval in seconds) dismissed notice.

How to Create a New Notice

Example wp-admin notices types.

1. Admin notice:

General code sample to create a notice with Give_Notices

Give()->notices->register_notice(array(
    'id' => '' // Value: unique notice id
    'type' => '' // Value: error/warning/success/info/updated
    'description' => '' // Value: notice message content
    'auto_dismissible' => '' // Value: true/false. if set to true then notice will auto class in 5 seconds
    'dismissible_type' => '' // Value: null/user/all
    'dismiss_interval' => '' // Value shortly/permanent/custom
    'dismiss_interval_time' => '' // Value: time interval in seconds (only need if dismiss_interval set to custom)
));

2. Frontend Notices:

Example frontend notice types.

  1. Render inline notice:

    give_output_error( 'error message', true, 'error' );
    give_output_error( 'warning message', true, 'warning' );
    give_output_error( 'success message', true, 'success' );
  2. Render error stored in session:

    give_set_error( 'error_one', 'Error: one' );
    give_set_error( 'error_two', 'Error: two' );
    give_set_error( 'error_three', 'Error: three' );
    
    // Render all session errors with this function
    // Call this function according to your DOM structure.
    Give()->notices->render_frontend_notices();

Moving Forward

Once the new notices class is released in 1.8.9 we will be refactoring our add-ons to use the new class. This will help to make our code DRYer and ensure we’re setting a good example for future Give developers. We hope you enjoy the new notice class refactor coming soon!

 

Ravinder Kumar

Open source lover, the father of a daughter, continually improving developer, and Senior WordPress Engineer at GiveWP.

 

One thought on “Give 1.8.9 Give_Notices Class Update

Leave a Comment