{"id":9189,"date":"2022-04-11T13:32:00","date_gmt":"2022-04-11T11:32:00","guid":{"rendered":"https:\/\/zeni.cz\/?p=9189"},"modified":"2024-07-10T10:55:29","modified_gmt":"2024-07-10T08:55:29","slug":"improve-wordpress-theme-and-plugin-development-using-design-patterns","status":"publish","type":"post","link":"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/wordpress-development\/improve-wordpress-theme-and-plugin-development-using-design-patterns\/","title":{"rendered":"Improve WordPress Theme and Plugin Development Using Design Patterns"},"content":{"rendered":"<div class=\"gb-container gb-container-24476be1\">\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong><strong>Do you want to create cleaner, more maintainable code, that is lot easier to change and build on in the WordPress environment? Giving some attention to concept of design patterns could make a massive difference here. If usage of design patterns is executed correctly during the development it can save you some time too.<\/strong><\/strong><\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-95ae4309\">\n\n<h2 class=\"wp-block-heading\">What Are Design Patterns? <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Design patterns help us <strong>to build<\/strong> our <strong>software<\/strong> (obviously including WordPress plugins and themes) on the <strong>knowledge of others<\/strong> before us. We don&#8217;t have to reinvent the wheel for solving some common problems that happened to many developers before us and we can focus on the specifics of our problem domain and customer requirements.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Design pattern<\/strong> is repeatable solution to commonly <strong>reoccurring problem<\/strong> in the software design. It cannot be used directly, but it provides us with template for <strong>solving the problem<\/strong> that can be reused in many similar situations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Studying<\/strong> design patterns and <strong>applying<\/strong> them when creating new code can drastically <strong>improve<\/strong> the<strong> structure<\/strong> and overall <strong>functionality<\/strong> and <strong>scalability<\/strong>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>After studying<\/strong> them you can also try to <strong>refactor<\/strong> your <strong>older code<\/strong> using <strong>design patterns<\/strong> and you will see how much more <strong>managable code<\/strong> could be.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-ac3cf886\">\n\n<h2 class=\"wp-block-heading\">Demystifying the Design Patterns Using Hooks <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Let&#8217;s start with something most of the WordPress developers are already familiar with and these are <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">hooks<\/a>, more specifically the <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/actions\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">actions<\/a> and <a href=\"https:\/\/developer.wordpress.org\/plugins\/hooks\/filters\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">filters<\/a> that are used in the WordPress a lot.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This is specific implementation of something called <strong>publisher-subscriber(pub-sub) pattern<\/strong>. In this pattern we define publishers, subscribers and a message broker:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>The publisher<\/strong> publishes a specific event, in our case by executing <strong><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/do_action\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">do_action<\/a><\/strong> or <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/apply_filters\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><strong>apply_filters<\/strong><\/a><strong>.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Subscribers <\/strong>can subscibe to specific events by calling <a target=\"_blank\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_action\/\" rel=\"noreferrer noopener nofollow\"><strong>add_action<\/strong><\/a> or <a target=\"_blank\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_filter\/\" rel=\"noreferrer noopener nofollow\"><strong>add_filter<\/strong><\/a>&nbsp;and execute an action when the event they are subscribed occurs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Message broker<\/strong>, in our case <strong>WordPress core <\/strong>make sure that all of the <strong>subscribers <\/strong>&nbsp;are notified about the publishers events they are subscribed to.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-8e174200\">\n\n<h3 class=\"wp-block-heading\"><strong><strong>Benefits of Publisher-Subscriber (pub-sub) Pattern<\/strong><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This gives us a lot of <strong>benefits<\/strong>. Publisher does not need to know anything about its subscriber and also subscribers does not need to know anything about publisher, as long as they can react to occurring event. Even if we <strong>deactivate the plugin<\/strong> that creates events or consumes the events, this will cause <strong>no errors<\/strong>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">I think that it is very <strong>useful <\/strong>not just consume hooks from WordPress core or plugins but also <strong>create hooks<\/strong> in our code and<strong> build<\/strong> our <strong>code around<\/strong> them. You are not just going to make other developers life easier if they decide to <strong>build on<\/strong> your <strong>plugin or theme<\/strong> functionality but trust me your life will also be much easier when implementing new functionality to your plugin or theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">There are many <strong>other smart design patterns<\/strong> that can be used in <strong>WordPress<\/strong>. They mostly require a bit <strong>more work<\/strong> to be applied then the hooks, but they can make a huge difference in design of your plugin or theme. Great example of the design pattern that can be used in the context of WordPress is <strong>singleton pattern<\/strong>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-2f9ea0ae\">\n\n<h2 class=\"wp-block-heading\">Singleton Pattern<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">This pattern requires knowledge of <strong>object-oriented programing<\/strong>. If you are not familiar with that, please study that first, and then you could come back.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Singleton pattern<\/strong> restricts initiation of certain class, so only one instance of the class can be created. This can be achieved by creating the private constructor, static property to store the instance and static function <code>getInstance()<\/code><em>.<\/em>This functions responsibility is to instantiate the object if it is not already instantiated and stored in the variable <code>$instance<\/code><em>. <\/em>You can see the <strong>example<\/strong> down bellow<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SingletonClass\n{\n    private static <strong>$instance<\/strong> = <em>null<\/em>;\n\n    private function <strong>__construct<\/strong>() {}\n\n    public static function <strong>getInstance<\/strong>()\n    {\n        if (<em>null<\/em> === <strong>self::$instance<\/strong>) {\n            <strong>self::$instance<\/strong> = <em>new<\/em> self();\n        }\n        return <strong>self::$instance<\/strong>;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Every time some code tries to create new instance of this class, error is triggered, because the constructor is private (this better be documented to avoid confusion), so the only way to get the instance is trough our <code>getInstance()<\/code> function<em>.&nbsp;<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\"><strong>Example usage<\/strong> for this would be to<strong> create a base class<\/strong> that responsibility is to instantiate all of our plugin functionality. We can bound for example <strong>configuration data<\/strong> to this instance, so this is accessible from anywhere in the plugin and it does not need to be fetched from database or <strong>config file<\/strong> multiple times, only on the single instantiation of this class.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-c8e3fd3b\">\n\n<h3 class=\"wp-block-heading\"><strong><strong>Don&#8217;t Overuse Singleton Pattern<\/strong><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">I think this pattern is a good example partially because it can be very <strong>tempting<\/strong> to <strong>overuse<\/strong> it, as it is easy to see the<strong> benefits<\/strong> of using it <strong>in many situations<\/strong>. I am not saying it should not be used but you have to <strong>consider drawbacks<\/strong> of the pattern too before using it.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">Main <strong>drawback<\/strong> of this pattern, in my opinion, is that every time we use this pattern we <strong>bound <\/strong>our <strong>functionality<\/strong> to direct knowledge about the singleton class. This is creating coupling of code and could cause that if we decide to change something in the singleton, we have to make <strong>many changes<\/strong> trough the project.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">So always <strong>think<\/strong> with caution<strong> before using<\/strong> this pattern or any other pattern, but if you are going to do a good job, this is going to be very <strong>rewarding to you<\/strong>.<\/p>\n\n<\/div>\n\n<div class=\"gb-container gb-container-2e95b78b\">\n\n<h2 class=\"wp-block-heading\">More Patterns <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">We have described two <strong>design patterns<\/strong> in this article. These patterns can be used in <strong>many situations<\/strong> during the <strong>WordPress development<\/strong> and could <strong>improve<\/strong> your <strong>code <\/strong>and thinking.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">But this is just a very <strong>small showcase<\/strong> of what could be done thanks to the concept of design patterns. There is a <strong>lot more<\/strong> design patterns out there that could be applied during the development. <strong>Studying<\/strong> them and <strong>applying<\/strong> them could make a huge difference during the development and could have very <strong>positive impact<\/strong> on what you are developing.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:16px\">I encourage you to give this a try either using well-known and high-quality literature such as <a href=\"https:\/\/www.amazon.com\/Design-Patterns-Elements-Reusable-Object-Oriented\/dp\/0201633612\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>or <a href=\"https:\/\/www.amazon.com\/Head-First-Design-Patterns-Brain-Friendly\/dp\/0596007124\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Head First Design Patterns<\/a> or on the websites such as <a href=\"https:\/\/refactoring.guru\/design-patterns\/catalog\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">refactoring.guru<\/a>, where a lot of <strong>patterns<\/strong> are well <strong>described<\/strong> including <strong>examples<\/strong> in <strong>PHP<\/strong>.<\/p>\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Do you want to create cleaner, more maintainable code, that is lot easier to change and build on in the WordPress environment? Giving some attention to concept of design patterns could make a massive difference here. If usage of design patterns is executed correctly during the development it can save you some time too.<\/p>\n","protected":false},"author":29,"featured_media":7250,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","footnotes":""},"categories":[312],"tags":[],"class_list":["post-9189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-development","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"author_meta":{"display_name":"rjurinek","author_link":"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/author\/rjurinek\/"},"featured_img":"https:\/\/zeni.cz\/app\/uploads\/2022\/04\/Zlepsite-vyvoj-WordPress-tem-a-pluginov-za-pouzitia-navrhovych-vzorov-300x200.jpeg","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/zeni.cz\/en\/blog-o-wordpress-woocommerce\/.\/wordpress-development\/\" class=\"advgb-post-tax-term\">WordPress Development<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">WordPress Development<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 4 years ago","modified":"Updated 2 years ago"},"absolute_dates":{"created":"Posted on 11.4.2022","modified":"Updated on 10.7.2024"},"absolute_dates_time":{"created":"Posted on 11.4.2022 13:32","modified":"Updated on 10.7.2024 10:55"},"featured_img_caption":"Zlep\u0161ite v\u00fdvoj WordPress t\u00e9m a pluginov za pou\u017eitia n\u00e1vrhov\u00fdch vzorov","series_order":"","_links":{"self":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/comments?post=9189"}],"version-history":[{"count":6,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9189\/revisions"}],"predecessor-version":[{"id":12408,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/posts\/9189\/revisions\/12408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/media\/7250"}],"wp:attachment":[{"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/media?parent=9189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/categories?post=9189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zeni.cz\/en\/wp-json\/wp\/v2\/tags?post=9189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}