0

I am trying to add google reCaptcha for the newsletter.

I have added the following code after <form>;

<button
class="g-recaptcha"
data-sitekey="6Lfc-WsUAAAAAMvtAnrdwdDGxdiOsuPyFK4wse1D"
data-callback="YourOnSubmitFn">
Submit
</button>

I want to call subscribe() function for parameter data-callback from file app/code/Magento/Newsletter/Model/Subscriber.php

How to call function from php to .phtml?

1 Answer 1

0
  1. Inject Subscriber object to your block.
  2. From file .phtml (template) call $block->subscribe()
3
  • I have tried, $subscribe = \Magento\Newsletter\Model\Subscriber; then, $subscribe->subscribe(); it is not working
    – 1990rk4
    Aug 24, 2018 at 7:24
  • No. inject Subscriber object to your block object. or using object Manager for create Subscriber instance Aug 24, 2018 at 8:44
  • Example: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $subscribe = $objectManager->create('Magento\Newsletter\Model\Subscriber'); $subscribe->subscribe(); //it works but not recommend Aug 24, 2018 at 8:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.