WP page redirects

Adding a page redirect through a code snippet. If the page name matches the string give the page is redirected to home.


add_action( 'wp', 'my_cystom_redirect' );
function my_cystom_redirect()
{
$slug = get_post_field( 'post_name', get_post() );
if ($slug == "mypage1" || $slug == "mypage2") {
     return esc_url( home_url() );
}
}

Sometimes it is difficult to tell what all the redirect rules on the site are, from the redirect plugins, custom code etc. Here is a quick code snippet that lists all the redirects stored in the system. The redirects will be listed directly on the page so you do want to put this code on your live site.

//rewrite rules
add_action( 'wp', 'show_all_redirects' );
function show_all_redirects()
{
    global $wp_rewrite;
echo nl2br('rules = '.print_r( $wp_rewrite->rules, true) . "\n");
}