is_email gives me error

时间:2020-03-20 作者:Excalibur Sword

正如您在该链接上看到的:

https://cixme.deniz-tasarim.site/video-upload/

单击提交按钮后,wp表示我的电子邮件无效,但我输入了有效的电子邮件。

Array ( [email_valid] => Email has no valid value )
相关代码:

  if(is_email($email)) {
            $error[\'email_valid\'] = "Email has no valid value";
        }
完整代码:

<?php
/* Template Name: Video Upload */

get_header();

    global $wpdb;

    if ($_POST) {

        $username = $wpdb->escape($_POST[\'txtUsername\']);
        $email = $wpdb->escape($_POST[\'txtEmail\']);
        $password = $wpdb->escape($_POST[\'txtPassword\']);
        $ConfPassword = $wpdb->escape($_POST[\'txtConfirmPassword\']);

        $error = array();
        if (strpos($username, \' \')!==FALSE) {
            $error[\'username_space\'] = "Username has Space";
        }
        if(empty($username)) {
            $error[\'username_empty\'] = "Needed Username must";
        }
        if(username_exists($username)) {
            $error[\'username_exists\'] = "Username already exists";
        }
        if(is_email($email)) {
            $error[\'email_valid\'] = "Email has no valid value";
        }
        if(email_exists($email)) {
            $error[\'email_existence\'] = "Email already exists";
        }
        if (strcmp($password, $ConfPassword) !==0) {
            $error[\'password\'] = "Password didn\'t match";
        }
        if (count($error) == 0) {
            wp_create_user($username,$password,$email);
            echo "User created successfully";
            exit();
        }
        else print_r($error);
    }

?>

        <form method="post">

    <p>
            <label for="txtUsername">Enter Username</label>
        <div>
            <input type="text" id="txtUsername" name="txtUsername" placeholder="Username"/>
        </div>
    </p>

    <p>
        <label for="txtEmail">Enter Email</label>
    <div>
        <input type="email" id="txtEmail" name="txtEmail" placeholder="Email"/>
    </div>
    </p>

    <p>
        <label for="txtPassword">Enter Password</label>
    <div>
        <input type="password" id="txtPassword" name="txtPassword" placeholder="Password"/>
    </div>
    </p>

    <p>
        <label>Enter Confirm Password</label>
    <div>
        <input type="password" id="txtConfirmPassword" name="txtConfirmPassword" placeholder="Confirm Password"/>
    </div>
    </p>
        <input type="submit" name="btnSubmit"/>
</form>

<?php get_footer(); ?>

1 个回复
SO网友:Excalibur Sword

我只是错过了“not”操作符(!) 在我的条件中(if(is_email($email))), 这反过来又导致有效的电子邮件地址被视为无效。所以我添加了! 这就解决了问题:

// code before; incorrect - missing the !
if(is_email($email)) {
    $error[\'email_valid\'] = "Email has no valid value";
}

// code after; working as expected after I added the !
if(! is_email($email)) { // if email is invalid, add the error message
    $error[\'email_valid\'] = "Email has no valid value";
}

相关推荐

初学者问题:通过管理Web界面访问Functions.php以导入自定义帖子类型?

是否可以访问这些功能。php文件仅仅使用管理web界面?我正在尝试访问以前创建的(手动编码的)自定义帖子类型,我不得不跳过很多障碍,因为我无法访问函数中的代码。php文件。我已经浏览了很多帮助页面,但建议的步骤似乎总是涉及到函数。php文件(我无法访问)或使用插件中的导入/导出工具,该插件首先创建了自定义帖子类型(据我所知,没有使用任何插件)。这似乎是一个非常基本的问题,但我一辈子都想不出来。任何帮助都将不胜感激!