acorn icon indicating copy to clipboard operation
acorn copied to clipboard

Cannot wp_redirect inside Components anymore

Open eavonius opened this issue 1 year ago • 2 comments

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

  1. Create an acorn component
  2. Add it to a blade template
  3. Add a call to wp_redirect to the constructor
  4. Hit the page

System info

No response

Log output

No response

Please confirm this isn't a support request.

No

eavonius avatar Feb 06 '24 22:02 eavonius

Can you please post some actual code needed to reproduce this?

retlehs avatar Feb 06 '24 22:02 retlehs

  1. 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>
    
  2. 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');
        }
    }
    
  3. Add the component to the index.blade.php for your theme:

    <x-forms.test-form />
    
  4. 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
    

eavonius avatar Feb 07 '24 19:02 eavonius

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

Log1x avatar Mar 18 '24 21:03 Log1x