{"id":11744,"date":"2020-06-23T09:47:54","date_gmt":"2020-06-23T04:17:54","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11744"},"modified":"2020-10-09T18:59:47","modified_gmt":"2020-10-09T13:29:47","slug":"magento-2-add-form-key-phtml-file","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-add-form-key-phtml-file\/","title":{"rendered":"Magento 2 &#8211; How to Add Form Key in phtml File"},"content":{"rendered":"<p>In this post I am going to explain how to add form key in phtml file.<\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">public function __construct(\r\n\t\\Magento\\Catalog\\Block\\Product\\Context $context,\r\n\t\\Magento\\Framework\\Data\\Form\\FormKey $formKey,\r\n\tarray $data = []\r\n) {\r\n\tparent::__construct($context, $data);\r\n\t$this-&gt;formKey = $formKey;\r\n}\r\n\r\npublic function getFormKey()\r\n{\r\n\t return $this-&gt;formKey-&gt;getFormKey();\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php echo $block-&gt;getBlockHtml('formkey')?&gt;<\/pre>\n<p>The result is as below and you will find hidden form_key field inside the form.<\/p>\n<pre class=\"lang:default decode:true \">&lt;input name=\"form_key\" type=\"hidden\" value=\"8KnYuhbhWYxH4Lch\"&gt;<\/pre>\n<p>That&#8217;s it. Enjoy Magento 2!!<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1776005033\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to add form key in phtml file. Add below code snippet in Block class. public function __construct( \\Magento\\Catalog\\Block\\Product\\Context $context, \\Magento\\Framework\\Data\\Form\\FormKey $formKey, array $data = [] ) { parent::__construct($context, $data); $this-&gt;formKey = $formKey; } public function getFormKey() { return $this-&gt;formKey-&gt;getFormKey(); } Add below code snippet in template&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-add-form-key-phtml-file\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11819,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11744"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11744"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11744\/revisions"}],"predecessor-version":[{"id":11769,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11744\/revisions\/11769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11819"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}