register_settings and admin_notices

View: New views
4 Messages — Rating Filter:   Alert me  

register_settings and admin_notices

by Ade Walker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Possibly more a support question, but I think the answer may be useful to
other hackers...

I'm re-writing some of my plugins to implement register_settings() for the
plugin's settings page, ie replacing the "old" $POST and nonces way. This
works fine, including the register_settings() callback function which I've
defined for sanitising and validating the user input.

However, I can't find a way to display a customised "updated" message to
display validation errors found when the callback function is run. For
example, the settings page has a reset option to revert all settings to
their defaults and, if this is set, I want to display an alternative to the
standard "Settings Saved" message, eg "All settings have been reset", or
something along those lines.

The problem I have is getting the various messages to display. Any pointers
for how I can do this? Is it a question of hooking into admin_notices, and
if so when (my attempt at doing this within the callback function haven't
worked), or is there a different approach I should be taking? Or am I
missing something blindingly obvious here?

Any pointers would be really useful.

Thanks!

Ade.
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: register_settings and admin_notices

by Mohammad Jangda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here's the code that displays the "updated" message (in options-head.php):

<?php if (isset($_GET['updated'])) : ?>
> <div id="message" class="updated fade"><p><strong><?php _e('Settings
> saved.') ?></strong></p></div>
> <?php endif; ?>
>

I don't see an easy way to change that.

You could use admin_notices to display the custom message and use javascript
to hide the default "Settings Saved" message (obviously a subpar solution).
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: register_settings and admin_notices

by Glenn Ansley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I haven't looked at the code to see if there's an API for that but
rather than using javascript you could hook in before that chunk of
code and unset $_GET['updated']. Then you could use the admin_notices
to construct your own message. (Check to make sure it's your plugin's
page before unsetting it though).

Glenn Ansley
http://fullthrottledevelopment.com
http://twitter.com/full_throttle
http://twitter.com/glennansley

On Fri, Oct 23, 2009 at 8:27 AM, Mohammad Jangda <batmoo@...> wrote:

> Here's the code that displays the "updated" message (in options-head.php):
>
> <?php if (isset($_GET['updated'])) : ?>
>> <div id="message" class="updated fade"><p><strong><?php _e('Settings
>> saved.') ?></strong></p></div>
>> <?php endif; ?>
>>
>
> I don't see an easy way to change that.
>
> You could use admin_notices to display the custom message and use javascript
> to hide the default "Settings Saved" message (obviously a subpar solution).
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: register_settings and admin_notices

by Ade Walker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

@ Mohammad and Glenn,

Thanks for the feedback guys. Your comments gave me some ideas which led to
the following solution:

A conditional inside the register_settings() callback function checks if the
Settings page Reset checkbox has been checked. If true, it adds a new option
to the options array, called 'just-reset' with a value of 'true'. Then in
the admin_notices function, if 'just-reset' is true, the function outputs
the customised message. If the Settings page is updated without Reset,
'just-reset' is set to 'false', and no custom message is displayed. The
admin_notices function also checks the plugin's pagehook before doing
anything (good point, Glenn).

Plus, I can use the same method to add other validation messages, if
required, all hooked into admin_notices.

This works well, functionality-wise. I guess the only downside is the db
write and retrieval of the 'just-reset' option, but I don't think this is
really an issue given it's only happening in admin.

If anyone can see a better way, the feedback will be very welcome.

Thanks!

Ade.





2009/10/23 Glenn Ansley <glenn@...>

> I haven't looked at the code to see if there's an API for that but
> rather than using javascript you could hook in before that chunk of
> code and unset $_GET['updated']. Then you could use the admin_notices
> to construct your own message. (Check to make sure it's your plugin's
> page before unsetting it though).
>
> Glenn Ansley
> http://fullthrottledevelopment.com
> http://twitter.com/full_throttle
> http://twitter.com/glennansley
>
> On Fri, Oct 23, 2009 at 8:27 AM, Mohammad Jangda <batmoo@...> wrote:
> > Here's the code that displays the "updated" message (in
> options-head.php):
> >
> > <?php if (isset($_GET['updated'])) : ?>
> >> <div id="message" class="updated fade"><p><strong><?php _e('Settings
> >> saved.') ?></strong></p></div>
> >> <?php endif; ?>
> >>
> >
> > I don't see an easy way to change that.
> >
> > You could use admin_notices to display the custom message and use
> javascript
> > to hide the default "Settings Saved" message (obviously a subpar
> solution).
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers@...
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers