How can I redirect a specific page in WordPress using code?
Asked on Oct 01, 2025
Answer
To redirect a specific page in WordPress using code, you can utilize the `template_redirect` action hook. This allows you to add a custom function in your theme's `functions.php` file to perform the redirection based on the page's conditions.
<!-- BEGIN COPY / PASTE -->
add_action('template_redirect', 'custom_page_redirect');
function custom_page_redirect() {
if (is_page('your-page-slug')) {
wp_redirect(home_url('/new-page-slug/'));
exit;
}
}
<!-- END COPY / PASTE -->Additional Comment:
- Replace "your-page-slug" with the slug of the page you want to redirect.
- Replace "/new-page-slug/" with the URL path you want to redirect to.
- Ensure you have a backup of your `functions.php` file before making changes.
- Use `exit;` after `wp_redirect()` to stop further execution of the script.
Recommended Links: