User27
User27

Reputation: 545

How to redirect page in Wordpress by coding

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

Upvotes: 0

Views: 1364

Answers (3)

Farid Movsumov
Farid Movsumov

Reputation: 12725

You can workarond this problem by redirecting page via javascript

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

Upvotes: 1

lyhoshva
lyhoshva

Reputation: 111

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

Upvotes: 0

Varun Kumar
Varun Kumar

Reputation: 478

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.

Upvotes: 0

Related Questions