https://github.com/truongwp/wp-redirect-with-error
Handle error when redirecting to other URL in WordPress.
https://github.com/truongwp/wp-redirect-with-error
error-handler error-handling redirection wordpress
Last synced: 7 months ago
JSON representation
Handle error when redirecting to other URL in WordPress.
- Host: GitHub
- URL: https://github.com/truongwp/wp-redirect-with-error
- Owner: truongwp
- Created: 2017-05-31T04:42:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-12T09:35:59.000Z (over 8 years ago)
- Last Synced: 2025-02-25T00:38:40.049Z (11 months ago)
- Topics: error-handler, error-handling, redirection, wordpress
- Language: PHP
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## WordPress redirect with error
This class handles the error when redirecting to other URL in WordPress.
Use simple URL parameter and nonce, don't use SESSION or COOKIE which not advised in WordPress.
### How to use?
Just include file `class-truongwp-redirect-with-error.php` in your theme or plugin or use Composer to install.
```
composer require "truongwp/wp-redirect-with-error >= 0.1.1"
```
Then create a function to store class instance globally.
```php
register_error( 'error-code', 'error-message' );
$errors->register_error( 'error-1', 'This is error 1' );
$errors->register_error( 'error-2', 'This is error 2' );
$errors->register_error( 'error-3', 'This is error 3' );
return $errors;
}
```
To pass error to redirect URL. Instead of:
```php
add_error( $url, 'error-2' );
wp_redirect( $new_url );
```
To display error, use this code:
```php
show_error();
```
To display only specific error, pass error code to `show_error()` method:
```php
show_error( 'error-1' );
```
To change markup of error when displaying, use `set_template()` method:
```php
register_error( 'error-1', 'This is error 1' );
$errors->register_error( 'error-2', 'This is error 2' );
$errors->register_error( 'error-3', 'This is error 3' );
// Set new error markup.
$errors->set_template( '
%2$s
' );
return $errors;
}
```
You can also change some other value, read the code and comments in `class-truongwp-redirect-with-error.php` file.
### Contributing
Contributor: [@truongwp](https://truongwp.com)
Bug reports or Pull requests are welcome.