1

The users have to be redirected to a custom URL which I already know (its part of the programming logic). I just need to know which WP function to use/ what php code to use to redirect the user to the new URL. This code will be executed within a php function in functions.php--> that function will first do some processing, and then send the user to the new URL

How do I do the above?

Note-- I tried using wp_redirect but it does not work.

This is the code I tried to use (that did not work)--

       $redirecturl = get_post_type_archive_link('property');
       echo "\n\n Redirect URL for property posts=" + $redirecturl;
       wp_redirect( "http://" .  $redirecturl + "?post_type=property&search_keyword=" + $search_keyword ;
5
  • Edit your post to show us the code you tried that didn't work. Commented Jun 5, 2013 at 12:59
  • @CharlesClarkson added code that did not work to the post...
    – Arvind
    Commented Jun 5, 2013 at 13:06
  • @CharlesClarkson wp_redirect only works when no data has already been sent-- i.e. it should be the first thing happening( thats also the reason for the error message "Cannot Modify header information-...." ) hence I cannot use it...
    – Arvind
    Commented Jun 5, 2013 at 13:20
  • 1
    The echo is the reason for the warning message. You cannot echo something and then redirect using wp_redirect(). Commented Jun 5, 2013 at 13:27
  • 1
    You can use Output Buffering, if you really need to echo something - take a look here: stackoverflow.com/questions/3766353/… Commented Jun 5, 2013 at 13:29

2 Answers 2

5

First, it is hard to believe that wp_redirect isn't working, below some (example) code how to use it:

function wpse101952_redirect() {
  global $post;

    if( /*SOME CONDITIONAL LOGIC*/ ) { //examples: is_home() or is_single() or is_user_logged_in() or isset($_SESSION['some_var'])

        wp_redirect( /*SOME SPECIFIC URL*/ );

        exit();
    }
}
add_action( 'template_redirect', 'wpse101952_redirect' );

Second, there would be the question if that is the right approach for your case, but for that to decide you should elaborate on what you are trying to do a little bit more.


edit:

The function get_post_type_archive_link() gives you the complete permalink, you don't need to add http:// and ?post_type=property:

   wp_redirect( $redirecturl . "?search_keyword=" . $search_keyword );
0
2

Running something similar to your code I get this warning in the error log:

Warning: Cannot modify header information - headers already sent by (output started at /xxx/wordpress/wp-content/themes/balance/functions.php:4) in /xxx/wordpress/wp-includes/pluggable.php on line 876

When I used this to test:

$redirecturl = 'google.com';
wp_redirect( 'http://' .  $redirecturl );
exit;

Everything works fine, so you might try:

$redirecturl = get_post_type_archive_link( 'property' );
wp_redirect( 'http://' .  $redirecturl . '?post_type=property&search_keyword=' . $search_keyword );
exit;

The exit code is recommended in the Function Reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.