Home › Forums › General Issues › Add admin notices when using acf/update_value › Reply To: Add admin notices when using acf/update_value
Another way to accomplish this is to use the global variable $_SESSION to store whatever you want the notice to be. I wrote a PHP class and wrapper function to keep track of the notices so you don’t have to write to the database.
<?php
/**
* Class to handle admin notices
*
*/
class BricNotices {
public function __construct() {
//Start the session
add_action( 'admin_init', [ $this, 'start_session' ] );
add_action( 'admin_notices', [ $this, 'display_notices' ] );
}
/**
* Add a notice
*
*
*/
public function add_notice( $text = '', $type = 'success' ) {
$_SESSION['bric_notices'][] = [
'type' => $type,
'text' => $text,
];
}
/**
* Display the notices
*
*
*/
public function display_notices() {
if ( !isset( $_SESSION['bric_notices'] ) && empty( $_SESSION['bric_notices'] ) ) {
return;
}
if ( is_array( $_SESSION['bric_notices'] ) ) {
foreach ( $_SESSION['bric_notices'] as $this->notice ) {
$this->print_notice();
}
}
//Delete the notices
unset( $_SESSION[ 'bric_notices' ] );
}
private function print_notice() {
//notice types = error, warning, success, info
printf( '<div class="notice notice-%s is-dismissible"><p>%s</p></div>', $this->notice['type'], $this->notice['text'] );
}
/**
* Enable the use of the global $_SESSION variable
*
*
*
*/
public function start_session() {
if ( !isset( $_SESSION )) {
session_start();
}
}
}
global $BricNotices;
$BricNotices = new BricNotices();
function add_bric_notice( $text = '', $type = 'success' ) {
global $BricNotices;
$BricNotices->add_notice( $text, $type );
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.