WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 years ago

#12738 accepted feature request

Notice Helper

Reported by: andddd Owned by: john316media
Milestone: Future Release Priority: lowest
Severity: minor Version:
Component: Plugins Keywords: has-patch
Focuses: Cc:

Description

It would be useful if the next WP release will contain notice helper function in plugins API or somewhere in WP utils.

All plugin developers are often using standard WP admin notices which is simply a line of HTML:

<div class="updated fade"><p>Some message.</p></div>

I didn't find any function to produce this HTML code so I'm using my own but it's boring to move same function from one plugin to another.

My notice helper code:

    function html_notice_helper($message, $type = 'updated', $echo = true) {

        $text = '<div class="' . $type . ' fade"><p>' . $message . '</p></div>';

        if($echo) echo $text;

        return $text;
    }

Attachments (1)

plugin.diff (1.9 KB) - added by john316media 3 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 jorbin4 years ago

  • Milestone changed from 2.9.3 to Future Release
  • Type changed from enhancement to feature request

I don't think there is a need for a function to output a div. If used it core, it would make the code harder to read in my opinion.

Also, 2.9.3 will be a bug fix release if necessary. And as we are past feature freeze for 3.0, I'm moving to future release milestone. I'll let someone else close it if they also think it has no place in core.

comment:2 andddd4 years ago

This function is like a selected/checked helper functions in WP.

comment:3 scribu3 years ago

I think this would be useful too. I would name it admin_notice() though.

comment:4 andddd3 years ago

The function that I mentioned in my ticket is just an example. admin_notice looks good.

comment:5 westi3 years ago

  • Owner westi deleted
  • Status changed from new to assigned

comment:6 john316media3 years ago

THe one issue with this is you still have to hook into admin_notices and have a function that calls this helper function to generate the message. What I do in my plugins is I create a global array, each element of the array is then another array consisting of the message type "updated" or "error" the the message. I then hook into admin_noteces once and itterate though the array echoing out the appropriatie divs and messages. Perhaps that might be something that could be implimented, then its as simple as

global $admin_notices;
$admin_notices[] = array('updated', 'Hello World!');

then core would handle the rest. Or you could have a function that you pass the message type and the message content into and it could add it to the global array for the developer than it's as easy as:

add_admin_notice('updated', 'Hello World!');

If this is something you think would work, I can attach a diff of the code I use in my plugins to do this.

Last edited 3 years ago by john316media (previous) (diff)

comment:7 john316media3 years ago

  • Cc john316media added

comment:8 john316media3 years ago

  • Keywords has-patch added
  • Owner set to john316media
  • Status changed from assigned to accepted

I opted to include a function that handles adding the message to the global array for the plugin author, to make their life easy. Though thats just one function and can easily be removed if necessary.

john316media3 years ago

comment:9 westi3 years ago

I really don't see why we need this.

Why do we need to store all these notices in another global rather than plugins hooking in a function as they have always done.

comment:10 john316media3 years ago

As is each message I need to show requires 4 lines of code

add_action('admin_notices', 'some_function_name');
function some_function_name() {
    echo '<div id="message" class="updated"><p>Hello World!</p></div>';
}

At its simplest, this way to make the same message all it would take is

add_admin_notice('updated', 'Hello World!');

What looks simpler to you? 4 lines becomes 1 line, multiply this by the number fo messages a given plugin needs to display and thats alot of extra typing saved for the plugin authoer, makes things easier and does this really overbloat core? I personally don't think so.

Last edited 3 years ago by john316media (previous) (diff)

comment:11 andddd3 years ago

westi, we need this because every plugin has this function and it kinda frustrating to copy-n-paste this stuff from plugin to plugin.

comment:12 andddd3 years ago

<div class="updated">Oh, c'mon westi</div>

comment:13 freshman663 years ago

  • Cc freshman66 added
Note: See TracTickets for help on using tickets.