2

I am trying to add captcha with my custom form in WordPress. so I use the plugin Securimage-WP CAPTCHA for this. the plugin is working fine but in my page where I need this captcha, I used if(is_user_logged_in()) for displaying different forms for logged in or logged out users. it gives me an error Fatal error: Call to undefined function show_form(). please help me to out from this problem. thanks in advance. my current code is below:

<?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(); ?>
7
  • Have you defined show_form functions? Commented Jan 3, 2017 at 7:00
  • yes function show_form($errors = array(), $values = array()) Commented Jan 3, 2017 at 7:06
  • I mean, have you define your show_form function? Like function show_form(){ echo "....."; }. Commented Jan 3, 2017 at 7:11
  • yes i defined my function as function show_form($errors = array(), $values = array()){ my form code} Commented Jan 3, 2017 at 7:14
  • 1
    Would you please move this function function show_form($errors = array(), $values = array()) { YOUR_CODE } in your functions.php and try it ? Commented Jan 3, 2017 at 7:21

1 Answer 1

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 } ?>

Would you please move this functions in your current theme functions.php ? And after check it. I hope it's working fine for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.