Giter Club home page Giter Club logo

persist-admin-notices-dismissal's Introduction

Persist Admin notice Dismissals

Latest Stable Version Total Downloads

Simple framework library that persists the dismissal of admin notices across pages in WordPress dashboard.

Installation

Run composer require collizo4sky/persist-admin-notices-dismissal

Alternatively, clone or download this repo into the vendor/ folder in your plugin, and include/require the persist-admin-notices-dismissal.php file like so

require  __DIR__ . '/vendor/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php'
add_action( 'admin_init', array( 'PAnD', 'init' ) );

or let Composer's autoloader do the work.

How to Use

Firstly, install and activate this library within a plugin.

Say you have the following markup as your admin notice,

function sample_admin_notice__success() {
	?>
	<div class="updated notice notice-success is-dismissible">
    	<p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );

To make it hidden forever when dismissed, add the following data attribute data-dismissible="disable-done-notice-forever" to the div markup like so:

function sample_admin_notice__success() {
	if ( ! PAnD::is_admin_notice_active( 'disable-done-notice-forever' ) ) {
		return;
	}
	
	?>
	<div data-dismissible="disable-done-notice-forever" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}
add_action( 'admin_init', array( 'PAnD', 'init' ) );
add_action( 'admin_notices', 'sample_admin_notice__success' );

Autoloaders

When using the framework with an autoloader you must also load the class outside of the admin_notices or network_admin_notices hooks. The reason is that these hooks come after the admin_enqueue_script hook that loads the javascript.

Just add the following in your main plugin file.

add_action( 'admin_init', array( 'PAnD', 'init' ) );

Usage Instructions and Examples

If you have two notices displayed when certain actions are triggered; firstly, choose a string to uniquely identify them, e.g. notice-one and notice-two

To make the first notice never appear once dismissed, its data-dismissible attribute will be data-dismissible="notice-one-forever" where notice-one is its unique identifier and forever is the dismissal time period.

To make the second notice only hidden for 2 days, its data-dismissible attribute will be data-dismissible="notice-two-2" where notice-two is its unique identifier and the 2, the number of days it will be hidden is the dismissal time period.

You must append the dismissal time period to the end of your unique identifier with a hyphen (-) and this value must be an integer. The only exception is the string forever.

To actually make the dismissed admin notice not to appear, use the is_admin_notice_active() function like so:

function sample_admin_notice__success1() {
	if ( ! PAnD::is_admin_notice_active( 'notice-one-forever' ) ) {
		return;
	}

	?>
	<div data-dismissible="notice-one-forever" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done 1!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}

function sample_admin_notice__success2() {
	if ( ! PAnD::is_admin_notice_active( 'notice-two-2' ) ) {
		return;
	}

	?>
	<div data-dismissible="notice-two-2" class="updated notice notice-success is-dismissible">
		<p><?php _e( 'Done 2!', 'sample-text-domain' ); ?></p>
	</div>
	<?php
}

add_action( 'admin_init', array( 'PAnD', 'init' ) );
add_action( 'admin_notices', 'sample_admin_notice__success1' );
add_action( 'admin_notices', 'sample_admin_notice__success2' );

Cool beans. Isn't it?

persist-admin-notices-dismissal's People

Contributors

afragen avatar w3guy avatar

Stargazers

WEBBAB avatar

Watchers

James Cloos avatar Ahmad Awais โŒ˜ avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.