2017-06-22 43 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; ?>