I am trying to add google reCaptcha for the newsletter.

I have added the following code after <form>;


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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