check url parameter if not redirect wordpress plugin
To check a URL parameter and redirect in a WordPress plugin, follow these steps:
Create a WordPress Plugin: Create a new folder in the
wp-content/plugins/
directory of your WordPress installation. Inside this folder, create a PHP file for your plugin.Define Plugin Header: Add the plugin header to the PHP file to provide information about the plugin. This includes details like the plugin name, description, version, and author.
php
/*
Plugin Name: Custom Redirect Plugin
Description: Redirects based on URL parameters.
Version: 1.0
Author: Your Name
*/
- Implement the Redirection Logic:
Add code to check the URL parameter and perform the redirection if necessary. Use the WordPress
init
action hook to execute this code during the initialization process.
```php function custom_redirect() { // Check if the 'redirect_param' parameter is present in the URL if (isset($_GET['redirect_param'])) { // Get the value of the 'redirect_param' parameter $redirect_url = esc_url($_GET['redirect_param']);
// Perform the redirection
wp_redirect($redirect_url);
exit;
}
}
add_action('init', 'custom_redirect'); ```
This code checks if the 'redirect_param' parameter is present in the URL. If found, it retrieves the parameter value, sanitizes it, and redirects the user to the specified URL.
Activate the Plugin: Log in to your WordPress admin dashboard, navigate to the "Plugins" section, and activate your custom plugin.
Testing: Append the parameter to a URL to test the redirection. For example:
http://yourwordpresssite.com/?redirect_param=http://destinationurl.com
Accessing the above URL should trigger the redirection based on the provided parameter.
Remember to replace 'redirect_param' with the actual parameter you want to check, and customize the redirection logic as needed for your specific use case.