{"id":48489,"date":"2016-05-07T15:41:52","date_gmt":"2016-05-07T15:41:52","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=48489"},"modified":"2024-02-21T14:32:09","modified_gmt":"2024-02-21T14:32:09","slug":"how-to-use-custom-jquery-plugin-in-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/","title":{"rendered":"How To Use Custom JQuery Plugin In Magento2"},"content":{"rendered":"\n<p>Magento2 uses Require Js to load javascript dependencies in the project, There are many benefits of using Require JS since you can can define which dependency to load on which page, \u00a0and also there is no need to include js in head, you can include js in body that makes the load time of the page more faster.<\/p>\n\n\n\n<p>We will learn in this article how to include custom jQuery plugin using require js. So I am assuming that you have basic understanding of how to create a module in Magento2 if not, please read our other articles related to module development .<\/p>\n\n\n\n<p>First we assume that our module name is Webkul\\CustomJquery , now create a file\u00a0requirejs-config.js inside following path from magento root\u00a0app\/code\/Webkul\/CustomJquery\/view\/frontend :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">var config = {\n    map: {\n        &#039;*&#039;: {\n            mycomponent: &#039;Webkul_CustomJquery\/js\/jqplugin&#039;\n        }\n    }\n};<\/pre>\n\n\n\n<p>Now create a jqplugin.js file on this path app\/code\/Webkul\/CustomJquery\/view\/frontend\/web\/js :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ define is used to register a module in require js \ndefine(&#091;\n          &quot;jquery&quot;\n     ], function($) {\n\n     console.log(&#039;called&#039;);\n        \/\/defining our plugin\n\t$.fn.mycomponent = function(options) {\n\t\t\n\t\t\/\/ get initialised data here\n\t\tconsole.log(options);\n\t\t\/\/ &#039;your plugin is ready do whatevent you want to do&#039;\t\t\t\n\t};\n});<\/pre>\n\n\n\n<p>In the above code I have created a plugin which name is mycomponent, this name must be the same name which was given in requirejs-config.js<\/p>\n\n\n\n<p>Now create a test.phtml file on this path app\/code\/Webkul\/CustomJquery\/view\/frontend\/templates add this code like below :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n$data =  json_encode(array(\n    &quot;name&quot;=&gt;&quot;John Doe&quot;,\n    &quot;company&quot;=&gt;&quot;Webkul&quot;\n));\n?&gt;\n\n&lt;script type=&quot;text\/x-magento-init&quot;&gt;\n\t{\n\t\t&quot;*&quot;:{\n\t\t\t&quot;mycomponent&quot;:{\n\t\t\t\t&quot;wkdata&quot;:&lt;?php echo $data ?&gt;\n\t\t\t}\n\t\t}\n\t}\n&lt;\/script&gt;<\/pre>\n\n\n\n<p><code>&lt;script type=\"text\/x-magento-init\"&gt; tag defines that On DOM ready it will load the defined components and initialize the configuration.<\/code><\/p>\n\n\n\n<p>In the above code &#8220;*&#8221; defines that component will be loaded with no binding to any html element, you can specify an element there too . &#8220;mycomponent&#8221; in the js component that will be loaded here and the plugin that will be called and &#8220;wkdata&#8221;, you can name it anything you want it contains the data, that will be used by the plugin .<\/p>\n\n\n\n<p>Note that the above data must be in json format else it will give error.<\/p>\n\n\n\n<p>Now call this phtml on any page in your project and plugin will work ,make sure you run these commands after adding above code in the terminal &nbsp; :<\/p>\n\n\n\n<p>php bin\/magento cache:flush<\/p>\n\n\n\n<p>php bin\/magento setup:static-content:deploy<\/p>\n\n\n\n<p>That&#8217;s all your plugin is ready to use, please try this and if you have any issues just comment below, I will try to add more on this in future.<\/p>\n\n\n\n<p>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento2 uses Require Js to load javascript dependencies in the project, There are many benefits of using Require JS since you can can define which dependency to load on which page, \u00a0and also there is no need to include js in head, you can include js in body that makes the load time of the <a href=\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,8,302],"tags":[],"class_list":["post-48489","post","type-post","status-publish","format-standard","hentry","category-jquery","category-magento","category-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Use Custom JQuery Plugin In Magento2<\/title>\n<meta name=\"description\" content=\"How to initialize and use jquery plugin in magento 2\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Use Custom JQuery Plugin In Magento2\" \/>\n<meta property=\"og:description\" content=\"How to initialize and use jquery plugin in magento 2\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-07T15:41:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-21T14:32:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ashutosh Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh Srivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\"},\"author\":{\"name\":\"Ashutosh Srivastava\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\"},\"headline\":\"How To Use Custom JQuery Plugin In Magento2\",\"datePublished\":\"2016-05-07T15:41:52+00:00\",\"dateModified\":\"2024-02-21T14:32:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\"},\"wordCount\":347,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"jQuery\",\"magento\",\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\",\"name\":\"How To Use Custom JQuery Plugin In Magento2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2016-05-07T15:41:52+00:00\",\"dateModified\":\"2024-02-21T14:32:09+00:00\",\"description\":\"How to initialize and use jquery plugin in magento 2\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Use Custom JQuery Plugin In Magento2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\",\"name\":\"Ashutosh Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ashutosh Srivastava\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ashutosh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Use Custom JQuery Plugin In Magento2","description":"How to initialize and use jquery plugin in magento 2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/","og_locale":"en_US","og_type":"article","og_title":"How To Use Custom JQuery Plugin In Magento2","og_description":"How to initialize and use jquery plugin in magento 2","og_url":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-05-07T15:41:52+00:00","article_modified_time":"2024-02-21T14:32:09+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ashutosh Srivastava","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ashutosh Srivastava","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/"},"author":{"name":"Ashutosh Srivastava","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970"},"headline":"How To Use Custom JQuery Plugin In Magento2","datePublished":"2016-05-07T15:41:52+00:00","dateModified":"2024-02-21T14:32:09+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/"},"wordCount":347,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["jQuery","magento","Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/","url":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/","name":"How To Use Custom JQuery Plugin In Magento2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2016-05-07T15:41:52+00:00","dateModified":"2024-02-21T14:32:09+00:00","description":"How to initialize and use jquery plugin in magento 2","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-use-custom-jquery-plugin-in-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Use Custom JQuery Plugin In Magento2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970","name":"Ashutosh Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ashutosh Srivastava"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ashutosh\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/48489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=48489"}],"version-history":[{"count":9,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/48489\/revisions"}],"predecessor-version":[{"id":423574,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/48489\/revisions\/423574"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=48489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=48489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=48489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}