PHP code example of helsingborg-stad / recaptcha-integration

1. Go to this page and download the library: Download helsingborg-stad/recaptcha-integration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

helsingborg-stad / recaptcha-integration example snippets




    define('G_RECAPTCHA_KEY', 'YOUR-RECAPTCHA-SITE-KEY');
    define('G_RECAPTCHA_SECRET', 'YOUR-RECAPTCHA-SECRET-KEY');




    use \HelsingborgStad\RecaptchaIntegration as Captcha;
    



    add_action('wp_enqueue_scripts', 'getScripts', 999);
    
    function getScripts(){
        Captcha::initScripts();
    }


 

    add_action('pre_comment_on_post', 'reCaptchaValidation');
    
    function reCaptchaValidation() {
        
        if (is_user_logged_in()) {
            return;
        }

        Captcha::initCaptcha();
    }




    namespace YourTheme\YourCommentLogicNameSpace;
    use \HelsingborgStad\RecaptchaIntegration as Captcha;
    
   /**
    * Class CommentsFrontEnd
    * @package YourTheme\YourCommentLogicNameSpace
    */
    class CommentsFrontEnd
    {
       /**
        * CommentFrontEnd constructor.
        */
        public function __construct()
        {
            add_action('wp_enqueue_scripts', array($this, 'getScripts'), 999);
        } 
        
       /**
        * Enqueue Google Captcha javaScripts
        */
        public static function getScripts(){
            Captcha::initScripts();
        }
    }    
        



    namespace YourTheme\YourCommentLogicNameSpace;
    use \HelsingborgStad\RecaptchaIntegration as Captcha;

   /**
    * Class CommentsBackEnd
    * @package YourTheme\YourCommentLogicNameSpace
    */
    class CommentsBackEnd
    {
        /**
         * CommentsBackEnd constructor.
         */
        public function __construct()
        {
            add_action('pre_comment_on_post', array($this, 'reCaptchaValidation'));
        }
        
        /**
         * Validate reCaptcha
         */
        public function reCaptchaValidation()
        {
            if (is_user_logged_in()) {
                return;
            }
    
            Captcha::initCaptcha();
        }
    }