如何通过编码在Wordpress中重定向页面

如何通过编码在Wordpress中重定向页面

问题描述:

I am using WordPress twentysixteen theme. There I have created a page template. In that I have created a login form. I want to redirect a user to the next page after successful login.

I tried:

header('Location: http://website.com/my-page/');
wp_redirect(get_page_by_title('My Page'));

saying error

headers already sent

我正在使用WordPress的twentysixteen主题。 我在那里创建了一个页面模板。 在那里我创建了一个登录表单。 我想在成功登录后将用户重定向到下一页。 p>

我试过了: p>

  header('Location:http:/  /website.com/my-page/');
wp_redirect(get_page_by_title('My Page')); 
  code>  pre> 
 
 

说错误 p> \ n

标题已发送 p> blockquote> div>

You can workarond this problem by redirecting page via javascript

echo '<script type="text/javascript">window.location = "/my-page/"</script>';
exit;

Page templates are not appropriate for writing redirects. Try to put your redirect logic in functions.php with an appropriate hook.

// Add the code in functions.php 

add_filter( 'login_redirect', 'wpso39502735_redirect', 10, 3 );

function wpso39502735_redirect() {

    // Add your logic here otherwise all requests will redirect to "My Page"
    return get_page_by_title('My Page');
}

If by any chance you must redirect after the page load, use Javascript instead.

Check that file has UTF-8 without BOM encoding and you dont print enything to browser.