{"id":12884,"date":"2022-01-17T14:09:32","date_gmt":"2022-01-17T14:09:32","guid":{"rendered":"https:\/\/getdevdone.com\/blog\/?p=12884"},"modified":"2025-03-30T22:17:16","modified_gmt":"2025-03-30T22:17:16","slug":"create-your-own-wordpress-plugin-in-4-easy-steps","status":"publish","type":"post","link":"https:\/\/getdevdone.com\/blog\/create-your-own-wordpress-plugin-in-4-easy-steps.html","title":{"rendered":"Create Your Own WordPress Plugin in 4 Easy Steps"},"content":{"rendered":"\n<p>Often, WordPress websites can be customized without writing a single line of code. An abundance of publicly available free and paid plugins for various needs is what makes the WordPress platform so popular. Still, there are cases where a line or two of custom PHP code is needed. Advanced WordPress users can easily find those bits of code online and even add them to their websites. However, this is where all kinds of problems begin to emerge.&nbsp;<\/p>\n\n\n\n<p>This guide was created for advanced WordPress users who have basic understanding of PHP and, ideally, some experience with it. Do you want to improve the functionality of your website without breaking it? Then, read on!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-add-code-snippets-to-your-website\"><strong>How to Add Code Snippets to Your Website<\/strong>&nbsp;<\/h2>\n\n\n\n<p>There are two ways to add something to your WordPress website:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install a WordPress plugin&nbsp;<\/li>\n\n\n\n<li>Add code directly to your theme\u2019s<a href=\"https:\/\/www.wpbeginner.com\/glossary\/functions-php\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> functions.php<\/a> file&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>There is an ongoing debate regarding whether adding code directly to the functions.php file is better than using a plugin. Some people even argue that adding all changes to the functions.php file is better for performance. Spoiler alert: it\u2019s not.&nbsp;<\/p>\n\n\n\n<p>Both approaches have their pros and cons, but today, we are going to focus on the first method. You\u2019ll learn how to structure your code, organize your files, as well as activate, run and test your plugins. And remember \u2013 <strong>always <a href=\"https:\/\/getdevdone.com\/blog\/how-to-backup-wordpress.html\" target=\"_blank\" rel=\"noopener\" title=\"\">create a backup of your website<\/a><\/strong> before installing anything. It will save you a lot of time if something goes wrong (and it almost always does)!&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"so-what-is-a-wordpress-plugin\"><strong>So, What Is a WordPress Plugin?&nbsp;&nbsp;<\/strong><\/h2>\n\n\n\n<p>To put it simply, a plugin is a piece of software made from one or more files that \u201cplugs into\u201d your WordPress site to extend its functionality. Under the hood, it\u2019s just lines of code written by someone else to provide new functionality or extend existing functionality on your website. From blogs and portfolios to online stores, portals and directories, we can create all kinds of websites using only one platform \u2013 WordPress. All of this is possible thanks to plugins.&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"499\" src=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins-1024x499.png\" alt=\"WordPress Plugins Directory\" class=\"wp-image-22785\" srcset=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins-1024x499.png 1024w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins-300x146.png 300w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins-768x374.png 768w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins-1536x748.png 1536w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221409\/wordpress-plugins.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"when-you-might-need-a-custom-plugin\"><strong>When You Might Need a Custom Plugin<\/strong>&nbsp;<\/h2>\n\n\n\n<p>So, if there are so many plugins freely available online, why would anyone want to create their own plugin? A lot of website owners need to customize their websites even further. Ready-to-use solutions are awesome, but there is always a thing or two that needs to be changed. <\/p>\n\n\n\n<p>And if you know how to do those things yourself \u2013 great! Now, you just need to know how to add your changes. A lot of WordPress users just add new code directly to the functions.php file and call it a day. While it might fix the problem in the short term, creating a separate plugin is almost always better. And here is why.&nbsp;<\/p>\n\n\n\n<p>Your functions.php will be overwritten with the next theme update, so your changes will definitely be lost at some point in the future. Some website owners use third-party plugins to add code snippets, but if you are looking for a more lightweight and customizable solution, consider creating a custom WordPress plugin instead. It\u2019s way easier than you think. In fact, it can be done in only 4 steps!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-anatomy-of-a-custom-plugin\"><strong>The Anatomy of a Custom Plugin<\/strong>&nbsp;<\/h2>\n\n\n\n<p>Did you know that you can see the underlying code of all plugins installed on your website? Just go to Plugins &gt; Plugin Editor and use the dropdown on the right to switch between plugins. Below the editor, you can find the documentation and, on the right, access all plugin folders and files. <strong>Do not edit<\/strong> those files!&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"436\" src=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin-1024x436.png\" alt=\"Plugin code\" class=\"wp-image-22787\" srcset=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin-1024x436.png 1024w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin-300x128.png 300w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin-768x327.png 768w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin-1536x655.png 1536w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221523\/anatomy-of-custom-plugin.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>While you should be extremely careful while browsing those files directly, it\u2019s a great way to see for yourself how your favorite plugins are structured. You\u2019ll quickly notice that experienced plugin developers split their code into multiple files, leave plenty of comments, etc. In general, here are the main parts of any custom plugin:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The main plugin file (this is the most important file out of all of them)&nbsp;<\/li>\n\n\n\n<li>The Main folder (where all of these files live)&nbsp;<\/li>\n\n\n\n<li>Folders for different file types&nbsp;<\/li>\n\n\n\n<li>Scripts (JavaScript files)&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stylesheets (CSS files)&nbsp;<\/li>\n\n\n\n<li>A README file that contains information about the other files in a directory&nbsp;<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow advgb-dyn-e517fdb6\">\n<p><strong>Pro tip:<\/strong> If you plan to create a really small plugin that contains a few chunks of PHP code, you will only need the main plugin folder with the main file inside.&nbsp;<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-your-plugin\"><strong>Create Your Plugin<\/strong>&nbsp;<\/h2>\n\n\n\n<p>Plugins solve all kinds of problems, but they can create problems too. That\u2019s why it\u2019s important to know and follow the best practices in plugin development.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-create-the-main-file-and-folder\">1. Create the Main File and Folder<\/h3>\n\n\n\n<p>Every plugin needs a separate folder with the main file inside. It will always be a PHP file with commented-out text that contains important information about your plugin.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php \n\/*Plugin Name: &lt;NAME&gt; \nPlugin URI:  &lt;http:\/\/LINK TO THE PLUGIN HOMEPAGE&gt; \nDescription: &lt;THE PURPOSE OF THIS PLUGIN&gt; \nVersion:     &lt;VERSION&gt; \nAuthor:      &lt;YOUR NAME&gt; \nAuthor URI:  &lt;YOUR WEBSITE &gt; \nLicense:     &lt;MOST WORDPRESS PLUGINS ARE RELEASED UNDER THE GPL&gt; \nLicense URI: &lt;https:\/\/LINK TO YOUR PLUGIN LICENSE&gt;*\/ \n\/*Your code snippet here*\/ \n?&gt; <\/pre>\n\n\n\n<p>WordPress uses this information to populate the plugins screen on your website:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"270\" src=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description-1024x270.png\" alt=\"Plugin description\" class=\"wp-image-22788\" srcset=\"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description-1024x270.png 1024w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description-300x79.png 300w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description-768x203.png 768w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description-1536x405.png 1536w, https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/30221642\/plugin-description.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Out of all of these key-value pairs, only the plugin name is required. And if you are not planning to distribute this plugin, you don\u2019t really need all of this information. However, we would also recommend adding a plugin description, so you can easily tell what this plugin does without even opening it (and your future self will thank you). <\/p>\n\n\n\n<p>Other information about the plugin can be found in the README.txt file that populates the plugin&#8217;s page in the plugin directory. Again, you don\u2019t have to add it if you are just creating a small WordPress plugin for your needs.&nbsp;<\/p>\n\n\n\n<p>Now, let\u2019s create a new folder with an empty file inside. Copy the code snippet above and add it to the new file. Give them both a meaningful name and include a prefix (.php) after the file\u2019s name. Once you have a folder with the main file ready, move it to the wp-content\/plugins directory on your website. <\/p>\n\n\n\n<p>Now, when you go to Plugins &gt; Plugin Editor, you should be able to see your plugin in a dropdown menu on the right. The plugin will be also visible on the main Plugins page. But it doesn\u2019t do anything yet because there is no actual code inside.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-add-php-code\">2. Add PHP Code<\/h3>\n\n\n\n<p>It\u2019s a good practice to split your code into multiple files. However, it depends on what you are trying to achieve. If you are just adding a few lines of PHP code, you can add it to the main file directly. You\u2019ll see that we saved some space in our template for code snippets.&nbsp;<\/p>\n\n\n\n<p>Now is a good time to add your functions to this file. You can use a plugin editor to do it or, better yet, edit the file on your computer using code editors (Visual Studio Code, Codespaces, Atom, etc.) or simple text editing software. Then, just move your files to the website when they are ready. For more complex plugins, consider creating additional PHP files and storing them in a separate folder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-add-custom-css-and-js\">3. Add Custom CSS and JS<\/h3>\n\n\n\n<p>Create stylesheets and scripts as separate files and enqueue them. To do that, we will use<a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_scripts\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> wp_enqueue_scripts<\/a> to add JavaScript files and<a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_style\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> wp_enqueue_style<\/a> to add CSS files. The add_action hook is used for enqueuing both stylesheets and scripts.&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function myfunction() {     \nwp_enqueue_style( $handle, $src, $deps, $ver, $media);}add_action( 'wp_enqueue_scripts', \u2018myfunction' ); \n}\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow advgb-dyn-e517fdb6\">\n<p><strong>Pro tip:<\/strong> Instead of creating everything from scratch, consider using free and ready-to-use<a href=\"https:\/\/github.com\/hlashbrooke\/WordPress-Plugin-Template\"> WordPress plugin templates<\/a> that can be found on GitHub.&nbsp;<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-activate-run-and-test-your-plugin\">4. <strong>Activate, Run, and Test Your Plugin<\/strong>&nbsp;<\/h3>\n\n\n\n<p>And finally, it\u2019s time to run your custom plugin! Navigate to the Plugins view, find your plugin, and click on Activate. Ideally, you would do it on a staging website to make sure everything works as expected. Don\u2019t forget to test it thoroughly. <\/p>\n\n\n\n<p>Even if you are 100% sure that everything is correct, some functions might be deprecated or incompatible with your current WordPress or PHP version. To see if there are no errors or warnings, check your PHP error log or use the<a href=\"https:\/\/wordpress.org\/plugins\/error-log-monitor\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> Error Log Monitor<\/a> plugin to see the latest messages from your PHP error log right on your Dashboard.&nbsp;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity advgb-dyn-c3f4f0ad\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"top-class-wordpress-development-services-from-psd2html\">Top-Class WordPress Development Services from GetDevDone <\/h2>\n\n\n\n<p><em>While building a simple WordPress plugin is a task that you can handle yourself, creating plugins to address serious business issues is something that requires professional expertise. That&#8217;s exactly what our WordPress development team can offer you. With 16+ years of industry experience and thousands of successfully completed WP projects, we know everything about the world&#8217;s most popular CMS.<\/em><\/p>\n\n\n\n<p><em><a href=\"https:\/\/getdevdone.com\/contact-us.html\" target=\"_blank\" rel=\"noreferrer noopener\">Reach out to <\/a>us with any WordPress-related request, from <a href=\"https:\/\/getdevdone.com\/wordpress-theme-development.html\" target=\"_blank\" rel=\"noopener\" title=\"building engaging custom themes\">building engaging custom themes<\/a> or <a href=\"https:\/\/getdevdone.com\/wordpress-theme-customization.html\" target=\"_blank\" rel=\"noopener\" title=\"modifying existing ones\">modifying existing ones<\/a> to performance and security optimization.<\/em><\/p>\n\n\n\n<p><em>Helping your business grow is our number one priority!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress is a highly extensible platform, with tons of plugins to address almost any task. Sometimes, however, installing a plugin can be an overkill. Instead, site owners can change a few lines of code themselves to achieve the purpose. If you want to know how to create a plugin with your own hands, this post is for you. <\/p>\n","protected":false},"author":6,"featured_media":12888,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","footnotes":""},"categories":[740,752],"tags":[760,822,782,814],"class_list":["post-12884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial","category-wordpress-development","tag-custom-development","tag-php","tag-plugin-development","tag-wordpress"],"acf":[],"aioseo_notices":[],"author_meta":{"display_name":"Valerie Muradian","author_link":"https:\/\/getdevdone.com\/blog\/author\/valeri"},"featured_img":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2-300x300.png","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/getdevdone.com\/blog\/category\/tutorial\" class=\"advgb-post-tax-term\">Tutorial<\/a>","<a href=\"https:\/\/getdevdone.com\/blog\/category\/wordpress-development\" class=\"advgb-post-tax-term\">WordPress development<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">Tutorial<\/span>","<span class=\"advgb-post-tax-term\">WordPress development<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/getdevdone.com\/blog\/category\/wordpress-development\" class=\"advgb-post-tax-term\">Custom development<\/a>","<a href=\"https:\/\/getdevdone.com\/blog\/category\/wordpress-development\" class=\"advgb-post-tax-term\">PHP<\/a>","<a href=\"https:\/\/getdevdone.com\/blog\/category\/wordpress-development\" class=\"advgb-post-tax-term\">Plugin development<\/a>","<a href=\"https:\/\/getdevdone.com\/blog\/category\/wordpress-development\" class=\"advgb-post-tax-term\">WordPress<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">Custom development<\/span>","<span class=\"advgb-post-tax-term\">PHP<\/span>","<span class=\"advgb-post-tax-term\">Plugin development<\/span>","<span class=\"advgb-post-tax-term\">WordPress<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 4 years ago","modified":"Updated 1 year ago"},"absolute_dates":{"created":"Posted on January 17, 2022","modified":"Updated on March 30, 2025"},"absolute_dates_time":{"created":"Posted on January 17, 2022 2:09 pm","modified":"Updated on March 30, 2025 10:17 pm"},"featured_img_caption":"","series_order":"","featured_image_urls":{"thumbnail_723x315":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2-400x315.png","thumbnail_723x315-2x":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2.png","thumbnail_723x315-3x":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2.png","thumbnail_770x510":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2.png","thumbnail_770x510-2x":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2.png","thumbnail_770x510-3x":"https:\/\/s3.amazonaws.com\/newblog.psd2html.com\/wp-content\/uploads\/2022\/01\/17125456\/Intro-preview-2.png"},"featured_post_color":"#f3a345","author_avatar":"https:\/\/secure.gravatar.com\/avatar\/5beb6f0817cc101d7122d201f933d94a4c68077bd00f26e4a1771c421a6b2fab?s=96&d=mm&r=g","author_position":"Web Developer & Content Writer","reading_time":"<span class=\"span-reading-time rt-reading-time\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 6<\/span> <span class=\"rt-label rt-postfix\">min read<\/span><\/span>","prev_post":{"slug":"best-ways-to-optimize-your-wordpress-website","name":"6 Ways to Optimize Your WordPress Website"},"next_post":{"slug":"choosing-best-wordpress-hosting-for-your-site-key-guidelines","name":"How to Choose the Best WordPress Hosting for Your Site in 2022: Key Guidelines"},"related_posts":["must-have-wordpress-affiliate-plugins-to-boost-your-marketing-efforts","3-main-reasons-to-use-wordpress-for-building-your-minimum-viable-product","headless-wordpress-guide-insights"],"_links":{"self":[{"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/posts\/12884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/comments?post=12884"}],"version-history":[{"count":47,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/posts\/12884\/revisions"}],"predecessor-version":[{"id":24618,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/posts\/12884\/revisions\/24618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/media\/12888"}],"wp:attachment":[{"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/media?parent=12884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/categories?post=12884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/getdevdone.com\/blog\/wp-json\/wp\/v2\/tags?post=12884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}