2016-05-17 69 views
0

我有我自己的一段代碼,它負責將數據添加到特定的自定義帖子類型中。下面是代碼:wordpress-從前端添加自定義發佈數據+ recaptcha

<?php 
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == "opinie-klientow") { 
    //store our post vars into variables for later use 
    //now would be a good time to run some basic error checking/validation 
    //to ensure that data for these values have been set 
    $content = $_POST['content']; 
    $post_type = 'opinie-klientow'; 
    $custom_field_1 = $_POST['testimonials-author']; 
    $current_date = date("m-d-y"); 
    $before_date = "Opinia" . " "; 

    //the array of arguements to be inserted with wp_insert_post 
    $new_post = array(
     'post_title' => $before_date . " " . $current_date . " " . $custom_field_1, 
     'post_content' => $content, 
     'post_status' => 'publish', 
     'post_type' => $post_type 
    ); 

    //insert the the post into database by passing $new_post to wp_insert_post 
    //store our post ID in a variable $pid 
    $pid = wp_insert_post($new_post); 

    //we now use $pid (post id) to help add out post meta data 
    if (!add_post_meta($pid, 'wpcf-testimonials-author', $custom_field_1, true)) { 
     update_post_meta($pid, 'wpcf-testimonials-author', $custom_field_1); 
    } 
} 

?> 
<form method="post" name="front_end" id="testimonialsform" action=""> 
    <input type="text" name="testimonials-author" class="form-control" placeholder="Imię i Nazwisko"/> 
    <textarea rows="4" name="content" class="form-control content" placeholder="Treść opinii"/></textarea> 
    <input class="antyspam" type="hidden" name="email" tabindex="-1"/> 
    <div class="g-recaptcha" data-sitekey="KEY-XXXX" data-theme="light" data-type="audio"></div> 
    <br/> 
    <input type="submit" class="btn btn-violet submit" value="Dodaj opinię"/> 
    <input type="hidden" name="action" value="opinie-klientow"/> 
</form> 

如果我想結合起來,與下面的一段代碼(負責驗證碼:

if ($_POST["g-recaptcha-response"]) { 
    $resp = $reCaptcha->verifyResponse(
     $_SERVER["REMOTE_ADDR"], 
     $_POST["g-recaptcha-response"] 
    ); 
} else { 
    $validated = false; 
    $errors['recaptcha'] = __("Kod reCAPTCHA nie został wprowadzony poprawnie. Spróbuj ponownie.", 'domain.com'); 
} 

我試着在我的代碼開始時將它添加但它只是不工作..

回答

0

是否使用WP Recaptcha?如果不安裝它,他們的配置屏幕上更新重新驗證碼site_key

,然後更新您的模板是這樣的(特別是驗證碼段)

<?php $reOptions = WPPlugin::retrieve_options("recaptcha_options"); ?> 
<div class="g-recaptcha" data-sitekey="<?php echo $reOptions['site_key']; ?>" data-theme="light" data-type="audio"></div> 

,並在你的PHP部分(負責的代碼中插入自定義後)

if(isset($_POST['response_field']) && !empty($_POST['response_field'])) { 

    $reOptions = WPPlugin::retrieve_options("recaptcha_options"); 
    $reCaptcha = new ReCaptcha($reOptions['secret']); 

    $response = $reCaptcha->verifyResponse (
     $_SERVER['REMOTE_ADDR'], 
     $_POST['response_field'] 
    ); 

    if (!$response->success) { 
     // re Captcha validation failed 
    } else { 
     // re Captcha success, do your stuff here 
     // In your case put your post insertion code here 
    } 

} else { 
    // re Captcha field itself missing 
} 
+0

我通過聯繫表格7集成使用驗證碼。我想避免安裝另一個recaptcha插件。如果我已經有這樣的解決方案,我應該使用你的PHP? –