1

Inside the recaptcha.php file of the Recaptcha Plugin on Joomla 3.6, I have the following code:

case '2.0':
            $theme = $this->params->get('theme2', 'light');
            $file  = 'https://www.google.com/recaptcha/api.js?hl=' . JFactory::getLanguage()->getTag() . '&render=explicit';

            JHtml::_('script', $file, true, true);

            $document->addScriptDeclaration('jQuery(document).ready(function($) {$(window).load(function() {'
                . 'grecaptcha.render("' . $id . '", {sitekey: "' . $pubkey . '", theme: "' . $theme . '"});'
                . '});});'
            );
            break;

Which creates the string on the rendered html page:

<script src="https://www.google.com/recaptcha/api.js?hl=en-US&amp;render=explicit" type="text/javascript"></script>

loading the api.js for the recaptcha. Since I am using Rocket Loader from Cloudflare and the Recaptcha doesn't work on the website, I would like to change the above line to this:

<script data-cfasync="false" src="https://www.google.com/recaptcha/api.js?hl=en-US&amp;render=explicit" type="text/javascript"></script>

where after the

  1. I used a variable $cloud = 'data-cfasync="false"'; and changed JHtml::('script', $file, true, true); to JHtml::('script', $cloud, $file, true, true);
  2. I tried JHtml::script data-cfasync="false" ($file, true, true);
  3. I tried JHtml::('script data-cfasync="false"', $file, true, true);
  4. I tried JHtml::('script'.'data-cfasync="false"', $file, true, true);

But nothing worked.......

I know some of them are stupid but I am not a real programmer as you have figured out already. How can I do that? Thanks George

1
  • In the future, when you have Joomla-specific questions, please post them in Joomla Stack Exchange. Now you have enough rep points to upvote Irfan's answer. Commented Nov 1, 2018 at 11:34

1 Answer 1

2

If you check the definition of that function you will see the last parameter allows you to set attributes - libraries/src/HTML/HTMLHelper.php

public static function script($file, $options = array(), $attribs = array()){
 ....
 ....
}

You can do it like below code -

JHtml::_('script', $file, array(), array('data-cfasync' => "false"));

But I would not suggest you do it in this way as you are changing core files and these changes will be lost in the next Joomla update. It is recommended you to see if you can do any of the below changes -

1) Check if Cloudflare has some options to exclude a particular script

2) you can create a new plugin using the same code as existing recaptcha plugin

3) You can create a plugin that removes that api.js and add the script with that custom attribute.

I hope this answers your question.

Update

Older Joomla version has a different definition so for your case, I would suggest you do as described below -

$script = '<script src="' . $file . '" data-cfasync="false"></script>';
$document = JFactory::getDocument();
$document->addCustomTag($script);
Sign up to request clarification or add additional context in comments.

9 Comments

I thank you very much Mr. Irfan for your reply. I changed JHtml::_('script', $file, false, false); with JHtml::_('script', $file, array(), array('data-cfasync' => "false")); as suggested but the output is still the same: <script src="google.com/recaptcha/api.js?hl=en-US&amp;render=explicit" type="text/javascript"></script> It didn't change anything. I must be doing something wrong.... I also agree with your suggestions but this is a trick that I would like to use elsewhere too, changing the code produced by JHtml::_
@its4yougr: I tested this code on joomla 3.8.11 and it worked. Can you tell me the exact version?
My Joomla version on this site is 3.4.8. The recaptcha plugin version is Captcha - ReCaptcha version 3.4.0. Does it have to do with Joomla version?
@its4yougr: I have updated code for the older version.
SUPER ! ! ! It worked ! ! ! I thank you very much.... How do I rate or I don't know.... Give points to you? I just press the up arrow? How does it work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.