Lookhappy Design

26 July 2017

Smart WP redirects

So I was trying to figure out how to redirect from a category page in WordPress to another page. It’s not too hard. To do it manually I just used the following:

function my_page_template_redirect()
{
if ( is_category( 'zero-emission-transport' ) ) {
$url = site_url( '/what_we_do/zero-emission-transport/' );
wp_safe_redirect( $url, 301 );
exit();
}

elseif ( is_category( 'clean-power' ) ) {
$url = site_url( '/what_we_do/clean-off-grid-power/' );
wp_safe_redirect( $url, 301 );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Though I have ended up using a plugin to enable the end client the ability to do it themselves in the future