The appropriate hook for handling redirects would be template_redirect
:
function wpse_331804_redirects() {
/**
* 1. Check if the URL has a parameter [redirect]
* 2. If NO, proceed to the next step
* 3. If YES, then get that parameter value and look into [redirects] table
* 4. If you found a row that has that value, then get the [redirect_to] value
* 5. Redirect to that URL [redirect_to]
*/
if (isset($_GET['redirect'])) {
// Get the parameter value from the URL
$redirect_from = $_GET['redirect'];
// Add the table prefix to the table name
$table_name = $wpdb->prefix . 'redirects';
// The SQL query
$query = "
SELECT redirect_to
FROM $table_name
WHERE redirect_from = '$redirect_from';
";
// Run the SQL query and get the results
$result = $wpdb->get_results($query, OBJECT);
// If there was a result then do the redirection and exit
if (wp_redirect($result[0]->redirect_to)) {exit;}
}
}
add_action( 'template_redirect', 'wpse_331804_redirects' );