How can WordPress be configured to redirect links with specific IDs to URLs stored in a database without the need for additional PHP files?
To configure WordPress to redirect links with specific IDs to URLs stored in a database without additional PHP files, you can use the "template_redirect" action hook to check for the ID in the URL, query the database for the corresponding URL, and then perform a redirect using the "wp_redirect" function.
add_action('template_redirect', 'custom_redirect');
function custom_redirect() {
if (is_numeric(get_query_var('p'))) {
$id = get_query_var('p');
$url = get_url_from_database($id);
if ($url) {
wp_redirect($url);
exit;
}
}
}
function get_url_from_database($id) {
// Implement your database query logic here to retrieve the URL based on the ID
return $url;
}
Keywords
Related Questions
- What alternative method can be used to capture user input in PHP scripts when executing via HTTP request?
- How can PHP be used to redirect URLs without changing the displayed link?
- What are the benefits of using DateTime objects for date and time comparisons in PHP, compared to extracting hours and minutes manually?