logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Page redirect function

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

This question has been answered.

Josh Cranwell | 06/16/12 at 3:31pm Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/16/12
    3:35pm
    Francisco Javier Carazo Gil says:

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

    • 06/16/12 4:05pm

      Josh Cranwell says:

      Thanks but rather after less code

  • avatar
    Last edited:
    06/16/12
    3:36pm
    Agus Setiawan says:

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

    • 06/16/12 4:05pm

      Josh Cranwell says:

      Thanks but rather after less code

  • avatar
    Last edited:
    06/16/12
    3:37pm
    Arnav Joy says:

    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>
    }



    ?>


    • 06/16/12 4:06pm

      Josh Cranwell says:

      Thanks, but was more after php rather than javascript

  • avatar
    Last edited:
    06/16/12
    3:52pm
    Jatin Soni says:

    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.

    Previous versions of this answer: 06/16/12 at 3:52pm

    • 06/16/12 3:55pm

      Josh Cranwell says:

      Thank you this is just what I'm after.

This question has expired.



Josh Cranwell, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.