如何在wordpress中添加自定义表单的简单图像验证码

时间:2017-01-03 06:55:04

标签: php wordpress captcha

我正在尝试使用自定义表单在WordPress中添加验证码。所以我使用插件Securimage-WP CAPTCHA。该插件工作正常,但在我需要此验证码的页面中,我使用if(is_user_logged_in())显示登录或注销用户的不同表单。它给了我一个错误致命错误:调用未定义的函数show_form()。请帮我解决这个问题。提前致谢。我目前的代码如下:

<?PHP
/* Template Name: bbb */

get_header();
if(is_user_logged_in()){
//echo "<script> window.location.href='".site_url()."'; </script>";


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$values  = array();
$errors  = array();
$values['name']    = @trim(stripslashes($_POST['contact_name']));
$values['email']   = @trim(stripslashes($_POST['email']));
$values['message'] = @trim(stripslashes(strip_tags($_POST['message'])));
if (empty($values['name'])) $errors['contact_name'] = 'Please enter your    name';
if (!preg_match('/^(?:[\w\d-]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $values['email'])) $errors['email'] = 'The email address supplied is invalid';
if (strlen($values['message']) < 20) $errors['message'] = 'Please enter a message longer than 20 characters';
if (sizeof($errors) == 0) {
    if (function_exists('siwp_check_captcha')) {
        // make sure plugin is enabled before calling function
        if (false == siwp_check_captcha($err)) {
            $errors['captcha'] = $err;
        }
    }
}
if (sizeof($errors) > 0) {
    show_form($errors, $values);
} else {
    // form code goes here, no errors & captcha was correct
    echo "<span style='font-size: 1.2em'><strong><em>Congrats, you win the  captcha solving challenge!</em></strong>";
}
}//if condition end
else {
show_form();
}
?>

<?php function show_form($errors = array(), $values = array()) { ?>

<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission.  Please correct the following   errors:</p>
 <ul>
<?php foreach($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
    <label for="contact_name">Your Name:
    <input type="text" name="contact_name" id="contact_name" class="input"  value="<?php echo htmlspecialchars(stripslashes(@$values['name'])) ?>" size="20" /></label>
</div>
    <br />
<div>
    <label for="email">E-mail:
    <input type="email" name="email" id="email" class="input" value="<?php     echo htmlspecialchars(stripslashes(@$values['email'])) ?>" size="25" /></label>
</div>
    <br />
<div>
    <label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(@$values['message'])) ?></textarea></pre></label>
</div>
    <br />
           <?php echo do_shortcode('[siwp_show_captcha]'); ?>
    <p>&nbsp;</p>
    <p>
           <input type="submit" value="Send Message" />
    </p>
</form>
<?php }}//user logged in if end  
get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

<?php

    function show_form($errors = array(), $values = array()) { ?>

            <?php if (sizeof($errors) > 0): ?>
            <p>There was a problem with your submission.  Please correct the following   errors:</p>
            <ul>
            <?php foreach ($errors as $error): ?>
                    <li><?php echo $error ?></li>
            <?php endforeach; ?>
            </ul>
        <?php endif; ?>

        <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
            <div>
                <label for="contact_name">Your Name:
                    <input type="text" name="contact_name" id="contact_name" class="input"  value="<?php echo htmlspecialchars(stripslashes(@$values['name'])) ?>" size="20" /></label>
            </div>
            <br />
            <div>
                <label for="email">E-mail:
                    <input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(@$values['email'])) ?>" size="25" /></label>
            </div>
            <br />
            <div>
                <label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(@$values['message'])) ?></textarea></pre></label>
        </div>
            <br />
        <?php echo do_shortcode('[siwp_show_captcha]'); ?>
            <p>&nbsp;</p>
            <p>
                   <input type="submit" value="Send Message" />
            </p>
        </form>
    <?php } ?>

请您在当前主题functions.php中移动此功能吗?并检查后。我希望它对你有用。