1

Is it possible to redirect a user in WP without the header/wp_redirect being before header()?

I have a plugin that allows users to manage content they have created on the site... for example, they can delete it or disable comments

It's quite basic so i'm just using a GET request.. ie.

?action=comments&status=disable

When this button is click the action is performed but nothing happens on the page and the url is now: www.domain.com/user_data?action=comments&status=disable what I want to do is on success redirect the user back to www.domain.com/user_data

I have tried wp_redirect() and header() but they give 'headers already sent warnings'

I'm not sure how i can put that before the header when this code is within my plugin.

    $query = $this->db->update(
      //do something
    );

    if($query == 0)
    {
      $this->setMessage('error', 'There was an error: ' . $this->db->print_error());
    }
    else
    {
      wp_redirect(site_url('/user_data/'));
    }
  • On the front-end the template_redirect hook could be used for redirects before headers are sent. – birgire Jul 4 '15 at 4:16
  • Doesn't seem to work it attempts it on every request.. function my_redirect($page) { wp_redirect($page);exit; } add_action('template_redirect', 'my_redirect') it seems it is specifically designed to load different templates. Any ideas? – Goodbytes Jul 4 '15 at 7:05
  • I tried adding a flag around wp_redirect which when unset is ignored, then hooked this onto init or template_redirect, then when I want to redirect I set the flag which should cause it work but it's saying headers have been sent. – Goodbytes Jul 4 '15 at 8:06
  • Please post your code in more context – s_ha_dum Jul 6 '15 at 4:01
1

It's simply not possible, so here is the work around I come up with

  public function redirect($url = false)
  {
    if(headers_sent())
    {
      $destination = ($url == false ? 'location.reload();' : 'window.location.href="' . $url . '";');
      echo die('<script>' . $destination . '</script>');
    }
    else
    {
      $destination = ($url == false ? $_SERVER['REQUEST_URI'] : $url);
      header('Location: ' . $destination);
      die();
    }    
  }
1

It may not be the correct approach, but I found that using wp_loaded does the trick and puts you back in the right spot; you have to set the right conditions to be sure the correct data is being handled. Then strip the url from the conditions before redirecting. Aside from condtions you can also apply capability checks.

add_action ('wp_loaded', 'clean_admin_referer');
function clean_admin_referer() {

if ( isset( $_REQUEST['condition1']) && isset($_REQUEST['condition2']) ) {

    if (current_user_can('administrator')) {

        <!-- do your database magic -->

        $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'condition1', 'condition2' ));

        wp_redirect($_SERVER['REQUEST_URI']);

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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