2017-01-03 31 views
2

我想添加captcha與我的自定義窗體在WordPress中。所以我用這個插件Securimage-WP CAPTCHA。該插件工作正常,但在我需要此驗證碼的頁面中,我使用if(is_user_logged_in())來顯示登錄或註銷用戶的不同表單。它給我一個錯誤致命錯誤:調用未定義的函數show_form()。請幫我解決這個問題。提前致謝。我目前的代碼如下:如何添加簡單的圖像captcha與自定義窗體在wordpress

<?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-]+\.?)[email protected](?:(?:[\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(); ?> 
+0

您是否定義了'show_form'函數? – purvik7373

+0

yes函數show_form($ errors = array(),$ values = array()) – Archana

+0

我的意思是,你有沒有定義你的'show_form'函數?像'function show_form(){echo「.....」; }'。 – purvik7373

回答

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動這個功能呢?並檢查後。我希望它適合你。

+0

謝謝#purvik7373 – Archana

相關問題