Code Monkey home page Code Monkey logo

easy-wordpress-admin-notifications's Introduction

Easy WordPress Admin Notifications

A simple class that allows you to set and display WordPress admin notifications (success, notice, warning, and info messages) in the dashboard.

One frustration with the standard WordPress notification hook (admin_notices) is that it isn't straightforward to add a new notification after certain events have occurred. What prompted me to write this was the fact that I couldn't easily create a new admin_notice as a result of something that happened during the save_post action hook. This is because the browser refreshes after that hook fires.

This class works by storing all notices in the options table. When the admin_notices action runs, we check for any stored messages, display them, and then clear them out of the options table.

See https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices for more details.

Requirements

PHP 5.4+ for short array syntax

Installation

It's recommended you use this as a must use WordPress plugin. Simply download the file and place it into your wp-content/mu-plugins directory. It should exist here:

/wp-content/mu-plugins/johns-admin-notices.php

WordPress will automatically load the file and you will then be able to use the functionality throughout your theme or plugin.

Or feel free to put this directly inside your theme or plugin and rename everything. I'm happy as long as you find it useful.

Usage

There are 4 different methods you can use to add a notice. Here is some example usage:

Johns_Admin_Notices::add_info('Example info');
Johns_Admin_Notices::add_error('Example error.');
Johns_Admin_Notices::add_success('Example success.');
Johns_Admin_Notices::add_warning('Example warning.');

This would result in the following output:

Image showing output of above code in WordPress admin

Feedback

This is just something I created because I found it useful for a project, but I would be happy to consider any feedback.

easy-wordpress-admin-notifications's People

Watchers

 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.