Title: Single.php
Last modified: May 10, 2020

---

# Single.php

 *  Resolved [TokoDaring.Com](https://wordpress.org/support/users/wpjakarta/)
 * (@wpjakarta)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/)
 * Hello
 * actually i’m looking for your single.php just to put google recaptcha code. but
   i can not find that file.
 * am i correct to put the code into main-loop.php that handle for displaying all
   single posts ?
 * it looking good so far. the captcha box displayed on the comment form.
 * thank you
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fsingle-php-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Theme Author [Veda](https://wordpress.org/support/users/vedathemes/)
 * (@vedathemes)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12810778)
 * Hi there,
 * 1. First of all, we must never directly insert custom codes into theme files.
   All your customization will be lost permanently on theme update (This is how 
   WordPress work, all theme files will be overwritten). Also, we should always 
   keep theme up to date to receive security and other updates.
 * There are many online tutorials which advise to insert custom codes at the end
   of theme’s functions.php file or some other file. It is a bad idea.
 * 2. For your specific problem (adding google reCAPTCHA) you may consider using
   some plugin [https://wordpress.org/plugins/search/reCAPTCHA/](https://wordpress.org/plugins/search/reCAPTCHA/)
 * 3. However, if you still want to use custom codes, either create a child theme(
   using plugin [Child Theme Configurator](https://wordpress.org/plugins/child-theme-configurator/))
   OR simply use [code snippet](https://wordpress.org/plugins/code-snippets/) plugin(
   Code snippet plugin is like virtual functions.php file of the theme, which will
   not be overwritten).
 * 4. For your specific question above, You can use specific theme hook depends 
   upon what code you are entering. If you want to add some custom JavaScript for
   reCaptcha verification you can use ‘wp_footer’ hook.
 * An example would be,
 *     ```
       function bayleaf_child_recaptcha_custom() {
           // Enter your custom code here.
       }
       add_action( 'wp_footer', 'bayleaf_child_recaptcha_custom' );
       ```
   
 * Please inform what specific code you want to enter, and I will inform the proper
   hook.
 * Note: Always backup your site before making PHP changes.
 * Thanks,
 *  Thread Starter [TokoDaring.Com](https://wordpress.org/support/users/wpjakarta/)
 * (@wpjakarta)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12812612)
 * hello
 * yes, actualy i have code snippet to add every additional function code, but i
   think i have wrong on enqueue script hook, because if i write the code into my
   code snippet it causing error.
 * this is my code, need your advice to fixed this.
 * // code i inserted into main-loop.php
    `wp_enqueue_scripts('google-recaptcha','
   https://www.google.com/recaptcha/api.js');`
 * //function.php
 *     ```
       // add google reCAPTCHA box before submit komentar
       function add_google_recaptcha($submit_field) {
           $submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="----site-key-----"></div><br>' . $submit_field['submit_field'];
           return $submit_field;
           }
           if (!is_user_logged_in()) {
   
       add_filter('comment_form_defaults','add_google_recaptcha');
       }
   
       // Cek google recaptcha, spammer go away
       function is_valid_captcha($captcha) {
           $captcha_postdata = http_build_query(array( 'secret' => '----secret-key-----', 'response' => $captcha, 'remoteip' => $_SERVER['REMOTE_ADDR']));
           $captcha_opts = array('http' => array( 'method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $captcha_postdata));
           $captcha_context  = stream_context_create($captcha_opts);
           $captcha_response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify" , false , $captcha_context), true);
           if ($captcha_response['success'])
               return true;
           else
               return false;
       }
   
       function verify_google_recaptcha() {
           $recaptcha = $_POST['g-recaptcha-response'];
           if (empty($recaptcha))
               wp_die( __("<b>Pesan Kesalahan:</b> Harap berikan centang pada box reCAPTCHA!!!<p><a href='javascript:history.back()'>« Back</a></p>"));
           else if (!is_valid_captcha($recaptcha))
               wp_die( __("<b>Maaf kami tidak menerima Spammer!</b>"));
           }
       if (!is_user_logged_in()) {
                   add_action('pre_comment_on_post', 'verify_google_recaptcha');
       }
       ```
   
 *  Theme Author [Veda](https://wordpress.org/support/users/vedathemes/)
 * (@vedathemes)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12812659)
 * Try following for script in code snippet,
 *     ```
       function bayleaf_child_recaptcha_custom() {
           wp_enqueue_scripts('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
       }
       add_action( 'wp_footer', 'bayleaf_child_recaptcha_custom' );
       ```
   
 * I hope other PHP code (in above second code block) is inserted properly in code
   snippet plugin. Please inform if it is not working.
 * Thanks,
 *  Thread Starter [TokoDaring.Com](https://wordpress.org/support/users/wpjakarta/)
 * (@wpjakarta)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12812839)
 * Hello, it error when the code added into snippet, but work in function.php
 * this is some of the debug info :
    `PHP Fatal error: Uncaught Error: Call to undefined
   function is_user_logged_in() in /snippet-plugin-path/snippet-plugin-name.php:
   21`
 * please note i also make correction `wp_enqueue_scripts` to `wp_enqueue_script`.
 *  Theme Author [Veda](https://wordpress.org/support/users/vedathemes/)
 * (@vedathemes)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12812961)
 * Ok. we are calling is_user_logged_in function too early. Try following code
 * Full code (including script enqueue code as well). Remove all previous code from
   code snippet and functions.php and try below mentioned code (enter in code snippet
   plugin).
 *     ```
       function bayleaf_child_recaptcha_custom() {
           wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
       }
       add_action( 'wp_footer', 'bayleaf_child_recaptcha_custom' );
   
       // add google reCAPTCHA box before submit komentar
       function add_google_recaptcha($submit_field) {
       	$submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="----site-key-----"></div><br>' . $submit_field['submit_field'];
       	return $submit_field;
       }
   
       // Cek google recaptcha, spammer go away
       function is_valid_captcha($captcha) {
       	$captcha_postdata = http_build_query(array( 'secret' => '----secret-key-----', 'response' => $captcha, 'remoteip' => $_SERVER['REMOTE_ADDR']));
       	$captcha_opts = array('http' => array( 'method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $captcha_postdata));
       	$captcha_context  = stream_context_create($captcha_opts);
       	$captcha_response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify" , false , $captcha_context), true);
       	if ($captcha_response['success'])
       		return true;
       	else
       		return false;
       }
   
       function verify_google_recaptcha() {
       	$recaptcha = $_POST['g-recaptcha-response'];
       	if (empty($recaptcha))
       		wp_die( __("<b>Pesan Kesalahan:</b> Harap berikan centang pada box reCAPTCHA!!!<p><a href='javascript:history.back()'>« Back</a></p>"));
       	else if (!is_valid_captcha($recaptcha))
       		wp_die( __("<b>Maaf kami tidak menerima Spammer!</b>"));
       }
   
       function bayleaf_child_add_captcha_actions() {
       	if ( ! is_user_logged_in() ) {
       		add_action('pre_comment_on_post', 'verify_google_recaptcha');
       		add_filter('comment_form_defaults','add_google_recaptcha');
       	}
       }
       add_action('init', 'bayleaf_child_add_captcha_actions');
       ```
   
    -  This reply was modified 6 years ago by [Veda](https://wordpress.org/support/users/vedathemes/).
 *  Thread Starter [TokoDaring.Com](https://wordpress.org/support/users/wpjakarta/)
 * (@wpjakarta)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12813561)
 * hello, it work now
 * thank you for your help
 *  Theme Author [Veda](https://wordpress.org/support/users/vedathemes/)
 * (@vedathemes)
 * [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12814398)
 * Great.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Single.php’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/bayleaf/1.4.9/screenshot.png)
 * Bayleaf
 * [Support Threads](https://wordpress.org/support/theme/bayleaf/)
 * [Active Topics](https://wordpress.org/support/theme/bayleaf/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/bayleaf/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/bayleaf/reviews/)

## Tags

 * [reCAPTCHA](https://wordpress.org/support/topic-tag/recaptcha/)

 * 7 replies
 * 2 participants
 * Last reply from: [Veda](https://wordpress.org/support/users/vedathemes/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/single-php-2/#post-12814398)
 * Status: resolved