Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Page redirect function WordPress

  • SOLVED

Hello,

I'm trying to build a function to redirect to the home url when a page is displayed.

Can anyone please help make this function work.

This is what I started but cant get it to work...

// PAGE REDIRECTS
function page_redirects() {
if ( is_page('buy-the-book') ) { wp_redirect( get_bloginfo('url') ); }
else if ( is_page('testimonials') ) { wp_redirect( get_bloginfo('url') ); }
else ( is_page('contact') ) { wp_redirect( get_bloginfo('url') ); }
exit;
}
add_action('init', 'page_redirects');


Any help would be much appreciated.

Thanks

Answers (4)

2012-06-16

Jatin Soni answers:

try with this code

Place below code to top (very 1st line) of header.php

<?php
if ( is_page('buy-the-book') || is_page('testimonials') || is_page('contact') ) {
header( get_bloginfo('url') ) ;
}
?>


This code should be at the first line of the header.php even before <DOCTYPE>

Just for your knowledge:

Redirect script usually doesn't work if you place after any html code. From function.php it wordpress usually add code after wp_header() which is loacated deep after html starts. While redirect script normally should be before any html code starts on the page.


Josh Cranwell comments:

Thank you this is just what I'm after.

2012-06-16

Francisco Javier Carazo Gil answers:

Try to use this one: http://wordpress.org/extend/plugins/redirection/


Josh Cranwell comments:

Thanks but rather after less code

2012-06-16

Agus Setiawan answers:

how about this plugin : http://wordpress.org/extend/plugins/quick-pagepost-redirect-plugin/


Josh Cranwell comments:

Thanks but rather after less code

2012-06-16

Arnav Joy answers:

add this to header.php


<?php

if ( is_page('buy-the-book') || is_page('testimonials') || is_page('contact') ) {

?>
<script>
location.href="<?php echo get_bloginfo('url') ?>";
</script>
}



?>



Josh Cranwell comments:

Thanks, but was more after php rather than javascript