2017-06-22 26 views
0

我有一個自定義的PHP窗體提交給API。我想將此表單添加到我的Wordpress網站的側邊欄中。當我嘗試自定義form.php頁面時,它工作正常,提交給API並重定向。WordPress的自定義窗體將不會重定向

當我將custom-form.php包含到我的sidebar.php中時,它不想重定向,但它仍然提交給API。

我知道標題需要在其他任何加載之前發生,但是如果我將它包含到頁面中,我怎麼才能加載它?

header('Location: http://example.com'); 

有沒有更好的方法來將我的表單添加到WordPress的側邊欄?

代碼看起來是這樣的:

<?php 
if (isset($_REQUEST['submitted'])) { 

    $errors = array(); 

    if (!empty($_REQUEST['name'])) { 
    $name= $_REQUEST['name']; 
    } 
    else {$errors[] = 'You forgot to enter your name';} 
} 

if (isset($_REQUEST['submitted'])) { 
    if (empty($errors)) { 
     include 'myapi.php'; 
     header('Location: http://example.com'); 
    } 
} 

//Rest of code.... 
//Output validation errors 
//HTML Form 

回答

0

你的index.php文件應該是這樣的。

ob_start(); 

/*content of your index.php here*/ 

ob_flush(); 

這會導致您的輸出緩衝應在任何發送/回顯到瀏覽器之前啓動。

或者你可以嘗試使用默認重定向功能另一種解決方案:

<?php wp_redirect(site_url()); exit; ?>