{"id":16947,"date":"2020-08-07T06:54:46","date_gmt":"2020-08-07T06:54:46","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=16947"},"modified":"2020-08-13T07:25:10","modified_gmt":"2020-08-13T07:25:10","slug":"introduction-to-plugin-development","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/introduction-to-plugin-development\/","title":{"rendered":"Introduction to Plugin Development"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Welcome to the Plugin Development Series. I hope this Series Will Help You Write a good plugin.<\/h2>\n\n\n\n<p class=\"has-text-color has-luminous-vivid-orange-color\">But First You need to understand <strong>Why we make Plugins<\/strong>?<\/p>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\">There is a rule in WordPress Development that: <strong>DO NOT TOUCH WORDPRESS CORE<\/strong>, This means that you do not edit the core WordPress files or functions that helps WordPress keep running. And also it overwrites each function with every update,  Any new function or changes you want to make can be only done through plugins. WordPress plugins can be a function (a single PHP file) or an entire program to add something or modify in main word press functionality. Plugins allow you to greatly extend the functionality of WordPress without touching the WordPress core itself.<\/p>\n\n\n\n<p class=\"has-large-font-size\">What Is A Plugin?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Plugins are packages of code that extend the core functionality of WordPress<\/h4>\n\n\n\n<p>WordPress plugins are made up of PHP code and can include other assets&nbsp;such as&nbsp;images, CSS, and JavaScript. By making your own plugin you are&nbsp;<em>extending<\/em>&nbsp;WordPress, i.e. building additional functionality on top of what WordPress already offers.<\/p>\n\n\n\n<p class=\"has-large-font-size\">Basics Of Plugin.<\/p>\n\n\n\n<p>At its simplest, a WordPress plugin is a PHP file with a WordPress plugin header comment. It\u2019s highly recommended that you create a directory to hold your plugin so that all of your plugin\u2019s files are neatly organized in one place.<\/p>\n\n\n\n<p>To get started creating a new plugin, follow the steps below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"239\" height=\"202\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/wordpress-file.png\" alt=\"\" class=\"wp-image-16953\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Navigate to the WordPress installation\u2019s&nbsp;<strong>wp-content<\/strong>&nbsp;directory.<\/li><li>Open&nbsp;the&nbsp;<strong>plugins<\/strong>&nbsp;directory.<\/li><li>Create a new directory and name it after the plugin (e.g.&nbsp;<code>plugin-name<\/code>).<\/li><li>Open&nbsp;the new plugin\u2019s directory.<\/li><li>Create a new PHP file (it\u2019s also good to name this file after your plugin, e.g.&nbsp;<code>plugin-name.php<\/code>).<\/li><\/ol>\n\n\n\n<p>Now that you\u2019re editing your new plugin\u2019s PHP file, you\u2019ll need to add a plugin header comment. This is a specially formatted PHP block comment that contains metadata about the plugin, such as its name, author, version, license, etc. The plugin header comment must comply with the&nbsp;<a href=\"https:\/\/developer.wordpress.org\/plugins\/the-basics\/header-requirements\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener nofollow\">header requirements<\/a>, and at the very least, contain the name of the plugin<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"254\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/Capture.png\" alt=\"\" class=\"wp-image-16954\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/Capture.png 534w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/Capture-300x143.png 300w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<p>That&#8217;s all you need, Your basic plugin setup is ready now go to the plugin option in your WordPress admin panel and activate your plugin. It won&#8217;t change anything as you have nothing written on it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Hooks In WordPress Plugin?<\/h2>\n\n\n\n<p><a href=\"http:\/\/wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress<\/a>\u00a0hook is a feature that allows you to manipulate a procedure without modifying the file on WordPress core. A hook can be applied both to action (action hook) and filter (filter hook)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Purpose of Hooks<\/h4>\n\n\n\n<p>The primary purpose of hooks is to automatically run a function. In addition, this technique also has the ability to modify, extend, or limit the functionality of a theme or plugin.<\/p>\n\n\n\n<p>Here is an example of a hook in WordPress:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"338\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/wordpress-hooks.png\" alt=\"\" class=\"wp-image-17247\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/wordpress-hooks.png 854w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/wordpress-hooks-300x119.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/wordpress-hooks-768x304.png 768w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">How to Use WordPress Hooks?<\/h4>\n\n\n\n<p>Using hooks in WordPress does require a bit of knowledge about\u00a0HTML\u00a0and PHP. However, even if you are a complete beginner, creating both action and filter hooks might not be as difficult as you think.<\/p>\n\n\n\n<p>You only need to go to your post page, then switch to the text editor. When you are there, you can paste the hooks that you have copied from other sites or created yourself.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating an Action Hook<\/h3>\n\n\n\n<p>To add an action hook, you must activate the\u00a0<strong>add_action ()<\/strong>\u00a0function in the WordPress plugin. This function can be activated by writing the patterns below in your functions.php file\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"193\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/action.jpg\" alt=\"Action Hook\" class=\"wp-image-17248\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/action.jpg 573w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/action-300x101.jpg 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><figcaption>Action Hook Example<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Creating an FilterHook<\/h4>\n\n\n\n<p>You can create a filter hook by utilizing&nbsp;<strong>apply_filters()<\/strong>&nbsp;function. The hook filter is used to modify, filter, or replace a value with a new one.<\/p>\n\n\n\n<p>Just like using an action hook, it also has a function that filters a value with the associated filter hook functions (<strong>apply_filter<\/strong>).<\/p>\n\n\n\n<p>What is more, it has the function to add a hook filter to be associated with another function (<strong>add_filter<\/strong>).<\/p>\n\n\n\n<p>Here is an example of a filter hook:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"565\" height=\"184\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/filter-hook.jpg\" alt=\"filter hook\" class=\"wp-image-17249\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/filter-hook.jpg 565w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/filter-hook-300x98.jpg 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><figcaption>Filter Hook<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Unhooking Actions and Filters<\/h3>\n\n\n\n<p>If you want to disable the command from&nbsp;&nbsp;<strong>add_action()<\/strong>&nbsp;or&nbsp;<strong>add_filter()<\/strong>&nbsp;in your WordPress code, you can use&nbsp;<strong>remove_action()<\/strong>&nbsp;and&nbsp;<strong>remove_filter()<\/strong>.<\/p>\n\n\n\n<p>These codes are basically a way to exclude certain actions or filter functions. It allows you to modify a plugin that has too many unnecessary hooks which might disrupt your site\u2019s optimization.<\/p>\n\n\n\n<p>Right now, you might ask this; \u201cwhy not just delete these unnecessary codes?\u201d<\/p>\n\n\n\n<p>Well, it certainly is a viable option if you use your own codes.<\/p>\n\n\n\n<p>However, in WordPress, you often work with someone else\u2019s plugins or themes. It means that you risk making a fatal error if you delete the incorrect lines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the Plugin Development Series. I hope this Series Will Help You Write a good plugin. But First You need to understand Why we make Plugins? There is a rule in WordPress Development that: DO NOT TOUCH WORDPRESS CORE, This means that you do not edit the core WordPress files or functions that helps&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6317,5568,6316],"class_list":["post-16947","post","type-post","status-publish","format-standard","hentry","category-php","tag-basic-plugin","tag-install-wordpress","tag-wordpress-plugin"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=16947"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16947\/revisions"}],"predecessor-version":[{"id":17250,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16947\/revisions\/17250"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=16947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=16947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=16947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}