Problem with redirect on Sage 10

Hi, i have a simple register page.

  Template Name: Register

I always create redirect by this way, but in Sage 10 i have a problem.

[previous exception] [object] (ErrorException(code: 0): Cannot modify header information - headers already sent by (output started at /home/owline/Local Sites/talenti/app/public/wp-includes/embed.php:353) at /home/owline/Local Sites/talenti/app/public/wp-includes/pluggable.php:1329)
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Cannot modify h...', '/home/owline/Lo...', 1329, Array)
#1 /home/owline/Local Sites/talenti/app/public/wp-includes/pluggable.php(1329): header('X-Redirect-By: ...')
#2 /home/owline/Local Sites/talenti/app/public/wp-includes/pluggable.php(1437): wp_redirect('http://talenti....', 302, 'WordPress')
#3 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/storage/framework/views/fa7b45ca1a8c583f94238dc6a31dd32a6453246b.php(3): wp_safe_redirect('http://talenti....')
#4 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/filesystem/Filesystem.php(107): require('/home/owline/Lo...')
#5 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/filesystem/Filesystem.php(108): Illuminate\\Filesystem\\Filesystem::Illuminate\\Filesystem\\{closure}()
#6 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/view/Engines/PhpEngine.php(58): Illuminate\\Filesystem\\Filesystem->getRequire('/home/owline/Lo...', Array)
#7 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/view/Engines/CompilerEngine.php(61): Illuminate\\View\\Engines\\PhpEngine->evaluatePath('/home/owline/Lo...', Array)
#8 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/view/View.php(139): Illuminate\\View\\Engines\\CompilerEngine->get('/home/owline/Lo...', Array)
#9 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/view/View.php(122): Illuminate\\View\\View->getContents()
#10 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/vendor/illuminate/view/View.php(91): Illuminate\\View\\View->renderContents()
#11 /home/owline/Local Sites/talenti/app/public/wp-content/themes/talenti/index.php(46): Illuminate\\View\\View->render()
#12 /home/owline/Local Sites/talenti/app/public/wp-includes/template-loader.php(106): include('/home/owline/Lo...')
#13 /home/owline/Local Sites/talenti/app/public/wp-blog-header.php(19): require_once('/home/owline/Lo...')
#14 /home/owline/Local Sites/talenti/app/public/index.php(17): require('/home/owline/Lo...')
#15 {main}

It’s Sage bug or my code is broken?

Well, you are doing the redirect inside the template. At that point some body data (HTML head and so on) is already sent. You should move these redirects into the controller or into a separate site plugin.

@strarsis Ok, i tested this. When i added redirect in boot function [app/Providers/ThemeServiceProvider.php] everything works, but i have a problem with setting conditions. As I mentioned - so far I have added if on the selected page template.

What is the best way to create redirect in Sage 10? I am a novice developer and I have a problem to understand the mechanics of sage 10. I don’t know how to configure it.

You’re running into the same problem you had here, and it has the same solution: Problem with change header location This is not an issue with Sage: It is a basic concept for WordPress and web development generally.

Redirects are done by sending redirect headers to the browser, and you can’t send headers again once output has begun. Any time you attempt to do so you’ll get a “headers already sent” error, which is exactly what you have here. If you look at the source code for wp_redirect you can see that it’s attempting to send headers:

You can modify headers using WordPress’s hooks for that (which are linked in the other post), but once output has begun (i.e. once you’re in a Blade template) it’s too late, so you need to hook those actions earlier. I’d recommend familiarizing yourself with WordPress’s list of actions:

Okay, I get it. What’s the best way to solve this? I have three pages from which the logged in user is redirected to another place. Should I perform actions in the functions.php file in this case and check the currently loaded page template there? is this the best solution?

This topic was automatically closed after 42 days. New replies are no longer allowed.