{"id":1337,"date":"2020-03-31T21:11:00","date_gmt":"2020-03-31T14:11:00","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1337"},"modified":"2020-06-21T09:29:48","modified_gmt":"2020-06-21T02:29:48","slug":"dissecting-a-plugin","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/dissecting-a-plugin\/","title":{"rendered":"How to Dissect a Plugin with Poor Documentation?"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p><strong>Pre-requisite:<\/strong> Knowledge about what is Action and Filter. You can <a href=\"https:\/\/pixelstudio.id\/blog\/what-is-actions-and-filters\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"learn about it here (opens in a new tab)\">learn about it here<\/a>.<\/p><\/blockquote>\n\n\n\n<p>Have you ever wanted to hook into a plugin, googled how to do it, but found no result?<\/p>\n\n\n\n<p>Yes, incomplete\/poor documentation is a common thing in WordPress development. <\/p>\n\n\n\n<p>In this article, we will take a look at a plugin called <a href=\"https:\/\/wordpress.org\/plugins\/events-manager\/\">Events Manager<\/a> that has 100,000+ active installations but has no documentation about its Hooks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: CTRL + SHIFT + F<\/h2>\n\n\n\n<p>Proper code editor like Sublime or VisualStudio Code has a functionality to <strong>search all files<\/strong>. Usually, the shortcut is CTRL (or \u2318 in Mac) + Shift + F.<\/p>\n\n\n\n<p>So, open the plugin with your code editor and search for one of these: <code>apply_filters<\/code> if you need to change a value or <code>do_action<\/code> if you want to insert some codes at a certain place.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"611\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-search-all.jpg\" alt=\"\" class=\"wp-image-1362\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-search-all.jpg 572w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-search-all-449x480.jpg 449w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption>Search result of &#8220;apply_filters&#8221; in Visual Studio Code<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Find What You Need<\/h2>\n\n\n\n<p>Scan through the filter names and see which one seems like the one you need. In my case, what I need is to add &#8220;IDR (Indonesian Rupiah)&#8221; to the Currency selection.<\/p>\n\n\n\n<p>Hmmm, that filter named <code>em_get_currencies<\/code> is suspicious. So I clicked on that to see the full code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"167\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-found.jpg\" alt=\"\" class=\"wp-image-1363\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-found.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-found-480x107.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>Found the code that handles Currency list<\/figcaption><\/figure>\n\n\n\n<p>That&#8217;s definitely what I&#8217;m looking for!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Hook into That Filter<\/h2>\n\n\n\n<p>You got the filter name and the parameter. Now, hook into that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'em_get_currencies', function( $currencies ) {\n\n  $currencies->names['IDR'] = 'IDR - Indonesian Rupiah';\n  $currencies->symbols['IDR'] = 'Rp';\n  $currencies->true_symbols['IDR'] = 'Rp';\n\n  return $currencies;\n} );<\/code><\/pre>\n\n\n\n<p>I opened the Events Manager settings and Voila! It&#8217;s there:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"190\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-result-1.jpg\" alt=\"\" class=\"wp-image-1365\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-result-1.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/03\/dissect-result-1-480x122.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>New currency is added to the Setting&#8217;s dropdown<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The example above will give you a general idea on how to customize a plugin.<\/p>\n\n\n\n<p>It&#8217;s true that the hook you need might not exist. But if you use a plugin with decently high installations, you can expect it to have lots of hooks.<\/p>\n\n\n\n<p>If it has low active installation, then it&#8217;s a risk to be using that plugin in the first place.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>If you have experience dissecting a plugin, let me know how it goes in the comment below \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A lot of plugins have incomplete guides and docs. But that shouldn&#8217;t stop you from figuring out it&#8217;s filters and actions. Click here to learn more.<\/p>\n","protected":false},"author":1,"featured_media":1361,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[31],"class_list":["post-1337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-plugin","tag-php"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1337"}],"version-history":[{"count":10,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":1640,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions\/1640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1361"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}