johngodley / redirection

Manage all your WordPress 301 redirects and monitor 404 errors
https://redirection.me
GNU General Public License v3.0
552 stars 139 forks source link

Ability to send error message without debug info or throwing 500 in console? #3361

Closed awakash closed 1 year ago

awakash commented 2 years ago

Hello,

I'm using the action add_action( 'redirection_create_redirect', 'mysite_prevent_dupe_redirect' ); to prevent an existing redirect from being created. I'm using wp_die to show the error within the wpl-error error/debug div. Any ideas on how not to show all the debug info except for the error text I'd like to appear; along with 500 error in the console?

johngodley commented 2 years ago

I don't really understand this. What do you mean by debug info, error text, and 500 error in the console?

awakash commented 2 years ago

When there's an exact match of an existing url in the db, I'd like to set an error in the UI and note that the redirect was not created because it already exists.

if ( $new_url == $existing_url ) {
            // Might be a better way to show the error?
            $error = new WP_Error(
                'Unable to create redirect!', _(' Redirect ' . $new_url . ' already exists.'), array( 'response' => '200' )
            );
            if ( is_wp_error($error) ){
                wp_die( $error );
            }
        }

Appears as:

Redirection_‹_American_Civil_Liberties_Union_—_WordPress

Is there a better way of handling the error?

johngodley commented 2 years ago

You may be better hooking into the core WordPress REST API functions. By calling wp_die you are forcing an error - there is no way around that.