WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 8 months ago

Last modified 8 months ago

#12738 closed feature request (wontfix)

Notice Helper

Reported by: andddd Owned by: john316media
Milestone: 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 4 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 @jorbin5 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 @andddd5 years ago

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

comment:3 @scribu5 years ago

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

comment:4 @andddd5 years ago

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

comment:5 @westi4 years ago

  • Owner westi deleted
  • Status changed from new to assigned

comment:6 @john316media4 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. If this is something you think would work, I can attach a diff of the code I use in my plugins to do this.

Version 0, edited 4 years ago by john316media (next)

comment:7 @john316media4 years ago

  • Cc john316media added

comment:8 @john316media4 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.

@john316media4 years ago

comment:9 @westi4 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 @john316media4 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 4 years ago by john316media (previous) (diff)

comment:11 @andddd4 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 @andddd4 years ago

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

comment:13 @freshman664 years ago

  • Cc freshman66 added

comment:14 follow-up: @chriscct78 months ago

  • Resolution set to maybelater
  • Status changed from accepted to closed

This isn't something that is really core territory, and the advantage posed is extremely minimal

comment:15 in reply to: ↑ 14 @DrewAPicture8 months ago

  • Milestone Future Release deleted
  • Resolution changed from maybelater to wontfix

Replying to chriscct7:

This isn't something that is really core territory, and the advantage posed is extremely minimal

I agree, and the current admin_notices hook covers adding notices nicely, albeit duplicative.

Note: See TracTickets for help on using tickets.