{"id":89540,"date":"2019-08-02T14:26:01","date_gmt":"2019-08-02T19:26:01","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=89540"},"modified":"2022-02-01T20:27:57","modified_gmt":"2022-02-02T02:27:57","slug":"develop-custom-widgets-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/","title":{"rendered":"Develop Custom WordPress Widgets"},"content":{"rendered":"\n<p>You\u2019ve likely used widgets in WordPress, as <a href=\"https:\/\/wpengine.com\/blog\/14-useful-wordpress-widgets-to-deck-out-your-sidebar\/\">handy ways to customize<\/a> certain areas on your website. Sometimes they do exactly what you need, but occasionally you might want them to have a slightly different appearance or functionality.&nbsp;<\/p>\n\n\n\n<p>Fortunately, <a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/widgets\/\">developing custom widgets<\/a> for WordPress is possible. These PHP objects can be created from scratch and implemented on your site, or collected into a plugin for use in multiple themes. Either way, creating your own widgets is not as hard as you might expect.&nbsp;<\/p>\n\n\n\n<p>In this article, we\u2019ll go over how to develop your own custom widgets, and walk through determining what your widget will be used for and what you\u2019ll need first. Let\u2019s get started!&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Determine What Your Widget Is For<\/h2>\n\n\n\n<p>There are a few things you\u2019ll need to think about before you start putting together your first custom widget. Planning ahead will help you create your niche piece of coding more effectively.&nbsp;<\/p>\n\n\n\n<p>Widgets can only be placed in <a href=\"https:\/\/wordpress.org\/support\/article\/wordpress-widgets\/\">widget-ready areas<\/a> of a WordPress website. So you\u2019ll want to make sure you know exactly where your widget will be implemented, and what its purpose will be. You should be able to pinpoint what will make your widget unique, as this will help you to better plan out its design.&nbsp;<\/p>\n\n\n\n<p>Depending on the theme you are using, widgets can usually be placed in sidebars, headers, and footers:<\/p>\n\n\n\n<p>Sometimes there are multiple options for one or more of these areas (such as a \u201cSidebar 1\u201d and \u201cSidebar 2\u201d, for example). No matter where it\u2019s placed, a widget essentially uses PHP object programming to produce a piece of HTML for front-end display and back-end administration.&nbsp;<\/p>\n\n\n\n<p>As you will be adjusting the <a href=\"https:\/\/codex.wordpress.org\/Widgets_API#Developing_Widgets\">WP_Widget<\/a> class on your site in order to make custom changes, you\u2019ll want to finalize the plan for your widget prior to diving in. If there are other elements to your widget, such as a contact form function or a feed of data, make sure you have all the extra information you require before starting.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Will I Need? Start With the Base Code for a Widget<\/h2>\n\n\n\n<p>There are two main ways to approach developing your custom widget. You can create a plugin that your widget will be located in, or you can paste your widget code directly into your <em>functions.php<\/em> file.<\/p>\n\n\n\n<p>When you choose the latter option, your widget will only be functional with your current theme. Therefore, in most cases the best choice is to use a custom plugin, which will enable you to use the widget with any theme.<\/p>\n\n\n\n<p>You will also need <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/\">a WordPress widget\u2019s base code<\/a>, a text editor for editing your code, access to your WordPress admin dashboard, and <a href=\"https:\/\/wpengine.com\/support\/sftp\/\">a Secure File Transfer Protocol (SFTP) client<\/a> for accessing your site\u2019s files directly. Once you have all the necessary ingredients in place, you\u2019re ready to make a widget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Start Building Your Custom WordPress Widget (In 3 Steps)<\/h2>\n\n\n\n<p>Once you decide what the purpose and function of your widget is going to be, you can begin to develop the code. You\u2019ll want to be familiar with the basics of <a href=\"https:\/\/php.net\/manual\/en\/language.oop5.php\">classes and objects in PHP5<\/a>, as well as <a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/widgets\/#anatomy-of-a-widget\">the anatomy of a widget<\/a>.<\/p>\n\n\n\n<p>To get started, you\u2019ll want to set up a new folder to hold your widget files. Place your new folder in your <em>wp-content\/plugins\/ <\/em>directory, and name it:<\/p>\n\n\n\n<p>Now you have a staging area for your new widget\u2019s code files.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Use a Base Code to Format Your Widget<\/h3>\n\n\n\n<p>While you can work from scratch if you\u2019d like, the easiest way to get started is to use a well-written base code. You can find the <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/\">WP_Widget class<\/a> in your WordPress site\u2019s file directory, located in<em> wp-includes\/class-wp-widget.php<\/em>.<\/p>\n\n\n\n<p>To create your custom widget, you\u2019ll be extending the existing class to execute new functions. You can also find <a href=\"https:\/\/github.com\/lion2486\/base_wp_widget\">widget base codes on GitHub<\/a>. While there are many programmers out there willing to share their code, be sure to review what you\u2019re using and make sure it meets the <a href=\"https:\/\/make.wordpress.org\/core\/handbook\/best-practices\/coding-standards\/\">WordPress coding standards<\/a>.<\/p>\n\n\n\n<p>Once you have your base code, you\u2019ll need to insert it into the file you just created, so that WordPress will <a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/widgets\/#registering-a-widget\">register your widget<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ register My_Widget<br>add_action( 'widgets_init', function(){<br> register_widget( 'My_Widget' );<br>});<\/pre>\n\n\n\n<p>The registration code is simple, and should be copied directly into your widget\u2019s file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Customize Your Widget&nbsp;<\/h3>\n\n\n\n<p>At this point, you can start customizing your widget to do whatever you\u2019d like. Naturally, the way you do this will vary depending on your goals. However, there are a few basics you will need to familiarize yourself with first, especially if you\u2019re new to development.<\/p>\n\n\n\n<p>One concept you\u2019ll want to understand <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/\">are WordPress hooks<\/a>. These enable you to \u2018hook in\u2019 to WordPress&#8217; core code, and enact a certain function at a specific point in time.&nbsp;<\/p>\n\n\n\n<p>There are <a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/widgets\/#your-widget-class\">four main elements<\/a> in the widget class that you\u2019ll need to work with. They include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Construct:<\/strong> This sets up what your widget <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/__construct\/\">will look like<\/a> in the administration panel.<\/li><li><strong>Widget: <\/strong>This will process your functions and <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/widget\/\">display the HTML code<\/a> on the front end of your site.<\/li><li><strong>Form: <\/strong>This is where you set up form fields, for <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/form\/\">retrieving option values<\/a> from your widget\u2019s database in the administration panel.<\/li><li><strong>Update: <\/strong>This is how your widget options get <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_widget\/update\/\">saved to the database<\/a>.<\/li><\/ul>\n\n\n\n<p>Even if your widget doesn\u2019t use form fields or have options that need to be updated to the database, it\u2019s still good programming practice to include the placeholder code for each element in your widget\u2019s structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Turn Your Widget into a Custom Plugin<\/h3>\n\n\n\n<p>Once you have crafted the specifics of your widget, you\u2019ll want to make sure you can use it like a plugin. To do that, you\u2019ll need to include a <a href=\"https:\/\/developer.wordpress.org\/plugins\/the-basics\/header-requirements\/\">header code<\/a> that tells WordPress how to treat this new piece of code.&nbsp;<\/p>\n\n\n\n<p>At the very top of your widget\u2019s file, you can place the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php<br>\/*<br>Plugin Name: New-Widget<br>Plugin URI: http:\/\/wordpress.org\/extend\/plugins\/#<br>Description: This is an example plugin&nbsp;<br>Author: Your Name<br>Version: 1.0<br>Author URI: http:\/\/example.com\/<br>*\/<\/pre>\n\n\n\n<p>After you add this code, save the file and upload the changes to your site. You\u2019ll now be able to go into your WordPress dashboard and find your plugin in the <em>Plugins <\/em>menu:<\/p>\n\n\n\n<p>At this point, you can activate the plugin. Then, you\u2019ll be able to access its settings in the <em>Appearance &gt; Widgets<\/em> menu in WordPress. All that\u2019s left is to start using your newly-modified widget!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Build Customized Digital Experiences on WP Engine<\/h2>\n\n\n\n<p>When you encounter a widget that\u2019s <em>almost<\/em> perfect, and you wish you could change it, you can use steps we\u2019ve covered here to customize your own WordPress widgets. Plus, you can employ this method to create new widgets with unique functionality quickly.<\/p>\n\n\n\n<p>Here at WP Engine, we have the <a href=\"https:\/\/developer.wordpress.org\/\">resources to help you<\/a> build top-notch WordPress plugins and widgets. We also have lots of options when it comes to <a href=\"https:\/\/wpengine.com\/fast-wordpress-hosting\/\">fast WordPress hosting<\/a> and <a href=\"https:\/\/wpengine.com\/plans\/\">secure hosting plans<\/a>, so you never have to worry about your site\u2019s performance!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You\u2019ve likely used widgets in WordPress, as handy ways to customize certain areas on your website. Sometimes they do exactly what you need, but occasionally you might want them to have a slightly different appearance or functionality.&nbsp; Fortunately, developing custom widgets for WordPress is possible. These PHP objects can be created from scratch and implemented<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":297,"featured_media":0,"template":"","resource-topic":[901],"resource-role":[895,896,906,897],"resource-type":[916],"class_list":["post-89540","resource","type-resource","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Develop Custom WordPress Widgets | WP Engine\u00ae<\/title>\n<meta name=\"description\" content=\"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Develop Custom WordPress Widgets | WP Engine\u00ae\" \/>\n<meta property=\"og:description\" content=\"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-02T02:27:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg\" \/>\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\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/\",\"name\":\"Develop Custom WordPress Widgets | WP Engine\u00ae\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2019-08-02T19:26:01+00:00\",\"dateModified\":\"2022-02-02T02:27:57+00:00\",\"description\":\"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Develop Custom WordPress Widgets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521\",\"name\":\"Erin Myers\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g\",\"caption\":\"Erin Myers\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Develop Custom WordPress Widgets | WP Engine\u00ae","description":"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Develop Custom WordPress Widgets | WP Engine\u00ae","og_description":"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2022-02-02T02:27:57+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2024\/05\/WPE-IMG-Thumbnail-1200x630-1.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/","name":"Develop Custom WordPress Widgets | WP Engine\u00ae","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2019-08-02T19:26:01+00:00","dateModified":"2022-02-02T02:27:57+00:00","description":"Widgets are a bit niche, but full of possibility. WP Engine takes an in-depth look into developing widgets for WordPress--start learning how today!","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/develop-custom-widgets-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"Develop Custom WordPress Widgets"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/3a22232b01de39dcf588fb8e421c0521","name":"Erin Myers","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd881e115bc28c81642ec61752db9981ece9ee8b4c81498a9b6276b9cdcaf5e6?s=96&d=mm&r=g","caption":"Erin Myers"}}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2019\/08\/shutterstock_1157354179.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Agency, Developer, Entrepreneur, Freelancer","topic":"<strong>Topics:<\/strong> WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/89540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/297"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=89540"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=89540"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=89540"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=89540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}