Make WordPress Core

Opened 6 years ago

Last modified 2 years ago

#34669 assigned enhancement

Custom back_link in wp_die and provide a filter

Reported by: eventualo Owned by: eventualo
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: General Keywords: dev-feedback has-patch needs-refresh
Focuses: Cc:


At the moment, inside _default_wp_die_handler function, the $args['back_link'] (boolean) set to true allow to show a generic javascript back link: <a href='javascript:history.back()'>.

I know that it's possibile to write own function handler using wp_die_handler filter, but accepting a custom back url, and not only a boolean, could be an easy way without rewrite all the function.

In some cases it could be useful, here is an example.
I wrote a plugin that creates custom roles with custom caps. I use the meta cap filter to let user of custom roles to edit users of other roles, but not admin users.
So when an user of custom roles tries to bulk edit/promote/delete admin and not-admin users together, the user gets a wp_die message like: You can’t edit that user..
When the user goes back with browser, the user will see again the old users page with old (and wrong) info, because the bulk action has edited properly the not-admin users and leave the admins untouch.

I think it could be useful to provide the back url to wp_die message, so you can load the updated version of previous page.

I propose that $args['back_link'] can accept 3 types of values:

  • false: no back link shown (as now);
  • true: show javascript back link shown (as now);
  • {url}: show passed url.

In this way I can use wp_die for custom messages in my plugin pages, like:
wp_die( 'Error message', '', array( 'back_link' => admin_url('whatever.php') ) );

Then, it could be useful to have a filter inside _default_wp_die_handler function to customise error messages of default dashboard pages.
We could use the arguments ($message, $title, $args) to provide context to the filter, e.g.:

apply_filters( 'default_wp_die_handler_back_link', $r['back_link'], $title, $args )
apply_filters( 'default_wp_die_handler_back_link_'.sanitize_key( $title ), $r['back_link'], $args )

Or maybe use wp_get_referer to add more context.

I hope my explanation has been clear.

Attachments (1)

34669.diff (1.0 KB) - added by eventualo 6 years ago.

Download all attachments as: .zip

Change History (5)

#1 @eventualo
6 years ago

  • Component changed from Administration to General

I attach a patch with a suggested edit.
I added a 4th accepted value for $args['back_link']:

  • 'referer': that shows the wp_get_referer url, if any, or the default javascript back, if not referer.

So, in my sample scenario as explained in first post, a possible filter to provide a back link to reload users' page in dashboard could be:

function my_custom_wp_die_handler_back_link ( $back_link, $message, $title, $args, $referer ) {

        if ( strpos( $referer, 'wp-admin/users.php' ) !== false ) {
                $back_link = admin_url('users.php');
        return $back_link;
add_filter( '_default_wp_die_handler_back_link', 'my_custom_wp_die_handler_back_link', 10, 5 );
Last edited 6 years ago by eventualo (previous) (diff)

6 years ago

#2 @eventualo
5 years ago

  • Keywords dev-feedback added

This ticket was mentioned in Slack in #core by noisysocks. View the logs.

2 years ago

#4 @isabel_brison
2 years ago

  • Keywords has-patch needs-refresh added
  • Owner set to eventualo
  • Status changed from new to assigned
Note: See TracTickets for help on using tickets.