0

I am trying to develop a plugin so that it could display a content like a small notification banner throughout all admin pages in WordPress. I just need to know they functional way and I could develop its design. I'm sorry to ask a questions without any code implementation but I tried my best to get some idea on it.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Ramesh
  • 57
  • 3
  • 10

1 Answers1

3

WordPress has some glorious things called Action Hooks, some of which fire on the admin page. Basically they're called in order all the time, so you can say "Hey WordPress, run X function on Y hook", and any where/any time that Y hook runs, X function will be triggered.

They have a handy one called admin_notices that you can drop notices into (similar to the "Post Updated" notices when you publish/update a page or post)

So first you write a function that echos a notice (note that currently you need to include the HTML markup for them)

function so51734745_admin_notice() { ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'My Plugin is Doing Something!', 'plugin-text-domain' ); ?></p>
    </div>
<?php }

Now you just hook it into the admin_notices hook:

add_action( 'admin_notices', 'so51734745_admin_notice' );

Note: It's generally considered a bad practice to have a persistent non-dismissable notices when your plugin is active. There are many plugins (I'll keep nameless) that have persistent notices until you complete X action. Unless it's critical, notices should be dismissable and or only fired under certain conditions.

With that note in mind, one of my plugins alerts users when they're using my plugin to override page/post content similar to this:

function so51734745_admin_notice() {
    if( $my_condition == true ){ ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e( 'My Plugin is Doing Something!', 'plugin-text-domain' ); ?></p>
        </div>
    }
<?php }

So it only fires while that condition is true. Keep that in mind if your plugin has a broad spectrum appeal. If every plugin is "important" with a "persistent notice", then no plugin is important and all notices are ignored.

If you need a different type of banner you can consider similar HTML output on another admin hook like admin_footer, but then you'll have to style it yourself with a CSS file that's properly enqueued via admin_enqueue_scripts.

Also note that the current iteration of Gutenberg (lamely) just hides admin notices on Gutenberg enabled page/post edit screens - so you'll need to work around that if that's an issue for you.

Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • What is the alternative to notices ? Let's say for example i need to display a div on all plugin pages (custom post type list, settings page, single cpt etc ) . – Ion T Mar 15 '23 at 13:25
  • @IonT - The latter part of that, using something like the `admin_footer` hook? Then you can drop whatever you want in there and show it based on whatever condition you want, like checking the current URL, or what screen is being used with `get_current_screen()`, etc – Xhynk Mar 15 '23 at 19:57
  • I already tried the ```admin_footer``` hook, it displays the output at the bottom of the desired page. Instead i am trying to display a block of code that behaves as a header for all the pages related to my plugin. Wordpress by default outputs ```
    ``` on admin pages and i want to display my content as a child to this div. I know i could use ```admin_footer``` and then with JS to append it to ```
    ``` but i wonder if there is a "cleaner" solution.
    – Ion T Mar 16 '23 at 08:05
  • All the default available hooks are here: https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request. I don't recall off the top of my head if any go directly in `#wpcontent` or not unfortunately. If not you will likely need to use some JS to move it around, but there may be an otherwise acceptable action hook as well! – Xhynk Mar 17 '23 at 16:30