acorn
acorn copied to clipboard
Cannot wp_redirect inside Components anymore
Version
4.0.2
What did you expect to happen?
Previously if I added a wp_redirect call in the constructor of a component, it would go to the new page. I usually do a check to know if it's a post or not (this is for a form post being handled by the component). Regardless, wp_redirect doesn't do anything. I do have exit; called right after it.
What actually happens?
The same page appears.
Steps to reproduce
- Create an acorn component
- Add it to a blade template
- Add a call to wp_redirect to the constructor
- Hit the page
System info
No response
Log output
No response
Please confirm this isn't a support request.
No
Can you please post some actual code needed to reproduce this?
-
Create a file under
resources/views/components/forms/test-form.blade.php
with the following:<form method="post"> <input type="text" name="test" /> <input type="submit" /> </form>
-
Create a file under
app/View/Components/Forms/TestForm.php
with the contents:<?php declare(strict_types=1); namespace App\View\Components\Forms; use Roots\Acorn\View\Component; use Illuminate\Http\Request; class TestForm extends Component { public function __construct(Request $request) { if ($request->isMethod("post")) { error_log("Redirecting"); wp_redirect('/thank-you/'); exit; } } public function render() { error_log("Rendering"); return $this->view('components.forms.test-form'); } }
-
Add the component to the index.blade.php for your theme:
<x-forms.test-form />
-
Bring up the page. Type "test" in the input field and click submit. The following error occurs in Wordpress (where this would simply redirect to the URL /thank-you/ before:
[07-Feb-2024 19:51:29 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /workspaces/**redacted**/wordpress/web/wp/wp-includes/class-wp-styles.php:214) in /workspaces/**redacted**/wordpress/web/wp/wp-includes/pluggable.php on line 1435 [07-Feb-2024 19:51:29 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /workspaces/**redacted**/wordpress/web/wp/wp-includes/class-wp-styles.php:214) in /workspaces/**redacted**/wordpress/web/wp/wp-includes/pluggable.php on line 1438
I am going to close this issue as nothing has changed in Acorn related to how view components are handled. If this was broken/changed at some point, it would've been done in an upstream change in Laravel or it could possibly be related to https://github.com/roots/sage/pull/3167