Single.php
-
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 to see the link]
-
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://www.ads-software.com/plugins/search/reCAPTCHA/
3. However, if you still want to use custom codes, either create a child theme (using plugin Child Theme Configurator) OR simply use code snippet 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,
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'); }
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,
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
towp_enqueue_script
.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 4 years, 6 months ago by Veda.
hello, it work now
thank you for your help
Great.
- The topic ‘Single.php’ is closed to new replies.