• Resolved madebyaris

    (@arissetia)


    Hi,
    i’ve tested your plugin and it’s working great. I currently wanted to re-design the captcha image, and I nailed.
    But the code is inside of the plugins, which I believe it’s not recommend to do that, and I would like to modify it via theme functions.php

    Here the screenshoot of the result:

    This is the code that I changed /core/CaptchaImageGenerator.class.php line 28 – 77 :

    /**
    		 * Generate the Captcha
    		 */
    		public function getImage() {
    			// the captcha
    			$captcha = $this->get();
    
    			// Create the image
    			$image = imagecreate( 105, 30 ); // 125
    			imagecolorallocate( $image, 203, 14, 25 );
    
    			// Positioning
    			$offsetLeft = 10;
    
    			for ( $i = 0; $i < strlen( $captcha ); $i ++ ) {
    				//imagettftext( $image, 20, rand( - 10, 10 ), $offsetLeft + ( ( $i == 0 ? 5 : 15 ) * $i ), 25, imagecolorallocate( $image, 200, 200, 200 ), $this->_font, $captcha[ $i ] );
    				imagettftext( $image, 16, rand( - 15, 15 ), $offsetLeft + ( ( $i == 0 ? 5 : 15 ) * $i ), 25, imagecolorallocate( $image, 255, 255, 255 ), $this->_font, $captcha[ $i ] );
    			}
    
    			ob_start();
    			imagepng($image);
    			$image = ob_get_contents();
    			ob_end_clean();
    
    			return '<img src="data:image/png;base64,'.base64_encode($image).'"/>';
    		}
    
            public static function validate($captcha_code, $captcha_hash){
                $Captcha = Captcha::getByHash($captcha_hash);
    
                if(!$Captcha || $captcha_code != $Captcha->getCode()){
                    return false;
                }
    
                return true;
            }
    
            public static function get_form_field($fieldname, $classes = ''){
                $Captcha = new CaptchaImageGenerator(6);
                $CaptchaItem = __('','f12-captcha').' '.$Captcha->getImage();
    
                $CaptchaSession = new Captcha();
                $CaptchaSession->setCode($Captcha->get());
                $CaptchaSession->save();
    
                $captcha = '<input type="hidden" id="'.esc_attr($fieldname).'_hash" name="'.esc_attr($fieldname).'_hash" value="' . esc_attr($CaptchaSession->getHash()) . '"/>';
                $captcha .= '<div class="'.$classes.' ars-bbcp"><label>'.$CaptchaItem.'</label><br><input type="text" id="'.esc_attr($fieldname).'" name="' . esc_attr($fieldname) . '" placeholder="'.__('captcha','f12-captcha').'" value="" /></div>';
    
                return $captcha;
            }
    	}

    As my pre-question, is it possible to move the modification code to the theme functions.php? Maybe I overlook the code and you already provide this feature.

    I’m gladly open to help you to create this functionality and make changing the color text & background captcha easier ??</img>.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @arissetia ,

    Thanks for your request. We implemented additional filters that will allow you to customize the form field.

    • f12-cf7-captcha-get-form-field-honeypot
    • f12-cf7-captcha-get-form-field-image
    • f12-cf7-captcha-get-form-field-math
    add_filter('f12-cf7-captcha-get-form-field-image', function($captcha, $fieldname, $CaptchaItem, $CaptchaSession, $classes){
       // Manipulate the captcha field here.
       return $captcha;
    });

    Hope that helps. If you need further assistance, do not hesitate to contact us.

    Best Regards
    Marc

    Thread Starter madebyaris

    (@arissetia)

    Thanks for your reply @forge12 that’s awesome ??
    Maybe you can add that filter information to the documentation or someplace that other dev can see it too ??

    Thread Starter madebyaris

    (@arissetia)

    Hi @forge12 ,

    I tested it and I don’t see any change of it,

    event when I intentionally to ‘break’ it, but I didn’t see issue with it.

    add_filter('f12-cf7-captcha-get-form-field-image', function($captcha, $fieldname, $CaptchaItem, $CaptchaSession, $classes){
        // Manipulate the captcha field here.
        echo "t4est";
        $captcha = 'make it error';
        return $captcha;
    });

    PS: I use it for Elementor Form, is the filter “should” be the same or not?
    Because I can’t find any apply_filter that related with the above.

    Hello @arissetia , please change your code to this:

    add_filter('f12-cf7-captcha-get-form-field-image', function($captcha, $fieldname, $CaptchaItem, $CaptchaSession, $classes){
        // Manipulate the captcha field here.
        echo "t4est";
        $captcha = 'make it error';
        return $captcha;
    }, 10, 5);

    That should fix it.

    Best Regards
    Marc

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Changing the style of captcha text’ is closed to new replies.