我想在我的footer.php
(稍后我会将其转换为插件),将两个表单字段发送到自定义表(wp_newsletter
). 我已经在正确地发送表单并将其写入表中,但我不知道如何将成功或失败消息发送回用户。我当前的代码如下:
<form method="post">
<input type="text" name="user_name">Name
<input type="text" name="user_email">Email
<input type="submit">
<?php echo $message; ?>
</form>
<?php
global $wpdb;
$table = $wpdb->prefix . "newsletter";
$name = sanitize_text_field( $_POST["user_name"] );
$email = sanitize_email( $_POST["user_email"] );
$message = "";
if( isset($_POST["submit"]) ) {
if ( is_email($email) && isset($name)) {
if ( $wpdb->insert( $table, array("name" => $name, "email" => $email)) != false ) {
$message = "Your subscription was sent.";
}
}
else {
if ( !is_email($email) ) {
$message = "Invalid email address.";
} elseif ( !isset($name) ) {
$message = "The field name is mandatory.";
} else {
$message = "Both name and email fields are mandatory.";
}
}
} else {
$message = "Please, try again later.";
}
?>
<?php wp_footer(); ?>
</body>
</html>
根据
$wpdb 文件说明:
如果无法插入行,此函数将返回false。否则,它将返回受影响的行数(始终为1)。