重新加载WP标头


WP header reload

我是wordpress插件开发的新手。我做了一些编码和理解足够关于制作菜单-子菜单等。在一个子菜单页面,我做了一个表单,改变了数据库中的一个mytable中的电子邮件id。但是,为了阻止数据插入页面重新加载/刷新/回页,我必须给出一个标题函数,但我无法做到这一点。

在更简单的话,我只是想停止wordpress插件插入/更新数据表在页面刷新。请阅读下面的代码。

<form method="post" enctype="multipart/form-data">
    <label><b>Mention Your Email Here :</b></label>
    <input type="email" name="email" id="email" placeholder="Enter your email here"/><br/><br/>
    <input type="submit" name="set" id="set" value="CHANGE"/>
</form>
<br/><br/>
<u>NOTE</u> :<br/>
1. Please write your email id in the field given above. This email id will be used to get emails of visitors contact query.
<br/>
2. Copy the shortcode given below to show the form on the website.<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<b>[myplugin]</b>
<?php
//getting the form email id value
if (isset($_POST['set']) && !empty($_POST['email'])) {
    $email = $_POST['email'];
    global $wpdb;
    $table_name = $wpdb->prefix . "myemail";
    $result = $wpdb->update($table_name, array('id' => '1', 'email' => $email), array('id' => '1'));
    if ($result > 0) {
        echo "<script>alert('Successfully Updated');</script>";
    } else {
        exit(var_dump($wpdb->last_query));
    }
    $wpdb->flush();
} else {
    header('location: ------?????--------------');
}
?>

在header中,我应该写什么来使页面在重新加载后回到相同的页面。但是没有插入/更新数据

您可以使用:

重定向到同一页面
header('Location: '.$_SERVER['REQUEST_URI']);
exit();

来自PHP手册:

"REQUEST_URI"为了访问这个页面而给出的URI;例如,'/index.html'。

要使header()工作,表单处理脚本需要在HTML输出之前运行,因此您需要将其挂钩到适当的WP动作挂钩(例如:"wp")。表单将在模板或包含中,处理函数通过WordPress add_action()连接。在保存到数据库之前,还需要对数据进行消毒。