WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#34669 new enhancement

Custom back_link in wp_die and provide a filter

Reported by: eventualo Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: General Keywords: dev-feedback
Focuses: Cc:
PR Number:

Description

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 4 years ago.

Download all attachments as: .zip

Change History (3)

#1 @eventualo
4 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:

<?php
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 4 years ago by eventualo (previous) (diff)

@eventualo
4 years ago

#2 @eventualo
3 years ago

  • Keywords dev-feedback added
Note: See TracTickets for help on using tickets.