PHP code example of stellarwp / admin-notices

1. Go to this page and download the library: Download stellarwp/admin-notices library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stellarwp / admin-notices example snippets


use StellarWP\AdminNotices\AdminNotices;

AdminNotices::initialize('my_plugin', plugin_dir_url(__FILE__) . 'vendor/stellarwp/admin-notices');

$container->set('StellarWP\AdminNotices\Contracts\NotificationsRegistrarInterface', function () {
    return new StellarWP\AdminNotices\NotificationsRegistrar();
});

AdminNotices::setContainer($container);

use StellarWP\AdminNotices\AdminNotices;
use StellarWP\AdminNotices\AdminNotice;
use \StellarWP\AdminNotices\DataTransferObjects\NoticeElementProperties;

AdminNotices::show('my_notice', 'This is a notice');
AdminNotices::show('my_notice', function (AdminNotice $notice, NoticeElementProperties $elements) {
    return 'This is a notice';
});

use StellarWP\AdminNotices\AdminNotices;

AdminNotices::removeNotice('my_notice');

use StellarWP\AdminNotices\AdminNotices;

$notice = new AdminNotice('my_notice', 'This is a notice');
AdminNotices::render($notice);

use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('edit.php');
    ->ifUserCan('manage_options')
    ->dismissible()

use StellarWP\AdminNotices\AdminNotices;

// Display the notice where the URL contains 'edit.php'
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('edit.php');

// Display the notice where the URL matches the regex
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on('~edit\.php~i');

// Display the notice on the 'edit-post' screen
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->on(['id' => 'edit-post']);

use StellarWP\AdminNotices\AdminNotices;

// Display the notice if the user can manage options
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan('manage_options');

// Display the notice if the user can manage options or edit posts
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan('manage_options', 'edit_posts');

// Display the notice if the user can edit post 1
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan(['edit_post', 1]);

// Display the notice via a UserCapability object
$capability = new StellarWP\AdminNotices\ValueObjects\UserCapability('manage_options');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->ifUserCan($capability);

use StellarWP\AdminNotices\AdminNotices;

// Display the notice after January 1, 2022, using a date parsable string
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after('2022-01-01');

// Display the notice after January 1, 2022, using a DateTime object
$date = new DateTime('2022-01-01');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after($date);

// Display the notice after January 1, 2022, using a timestamp
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->after(1640995200);

use StellarWP\AdminNotices\AdminNotices;

// Display the notice until January 1, 2022, using a date parsable string
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until('2022-01-01');

// Display the notice until January 1, 2022, using a DateTime object
$date = new DateTime('2022-01-01');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until($date);

// Display the notice until January 1, 2022, using a timestamp
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->until(1640995200);

use StellarWP\AdminNotices\AdminNotices;

// Display the notice between January 1, 2022, and January 31, 2022, using date parsable strings
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->between('2022-01-01 00:00:00', '2022-01-31 23:59:59');

use StellarWP\AdminNotices\AdminNotices;

// Display the notice if the current user is an administrator
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->when(function () {
        $user = wp_get_current_user();
        return in_array('administrator', $user->roles);
    });

use StellarWP\AdminNotices\AdminNotices;

// Automatically wrap the message in a paragraph tag
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->autoParagraph();

// Do not automatically wrap the message in a paragraph tag
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->autoParagraph(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->withoutAutoParagraph();

use StellarWP\AdminNotices\AdminNotices;

// Set the notice urgency to 'success'
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->urgency('success');

// The StellarWP\AdminNotices\ValueObjects\Urgency class can also be used
$urgency = new StellarWP\AdminNotices\ValueObjects\Urgency('success');
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->urgency($urgency);

use StellarWP\AdminNotices\AdminNotices;

// Use the alternate WordPress notice styles
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->alternateStyles();

// Use the standard WordPress notice styles, only necessary to revert back
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->alternateStyles()
    ->standardStyles();

use StellarWP\AdminNotices\AdminNotices;

// Display the notice inline
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->inline();

// Display the notice in the standard location
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->inline(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->notInline();

use StellarWP\AdminNotices\AdminNotices;

// Make the notice dismissible
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->dismissible();

// Make the notice not dismissible
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->dismissible(false);

// Also has an alias for readability
$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->notDismissible();

use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice_custom', 'This is a notice')
    ->custom();

use StellarWP\AdminNotices\AdminNotices;

// Display the notice above the header
$notice = AdminNotices::show('my_notice_custom', 'This is a notice')
    ->custom()
    ->location('above_header');

use StellarWP\AdminNotices\AdminNotices;
use StellarWP\AdminNotices\AdminNotice;
use \StellarWP\AdminNotices\DataTransferObjects\NoticeElementProperties;

$renderCallback = function (AdminNotice $notice, NoticeElementProperties $elements) {
    return "
        <div>
            <p>This is a custom notice</p>
            <button type='button' {$elements->closeAttributes()}>
                <span class='screen-reader-text'>Dismiss this notice.</span>
            </button>
        </div>
    ";
};

AdminNotices::show('my_notice', $renderCallback)
    ->custom();

use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->enqueueScript('https://example.com/my-script.js', ['jquery']);

use StellarWP\AdminNotices\AdminNotices;

$notice = AdminNotices::show('my_notice', 'This is a notice')
    ->enqueueStylesheet('https://example.com/my-style.css');

use StellarWP\AdminNotices\AdminNotices;

AdminNotices::resetNoticeForUser('my_notice', get_current_user_id());

use StellarWP\AdminNotices\AdminNotices;

AdminNotices::resetAllNoticesForUser(get_current_user_id());