{"id":34,"date":"2012-06-29T18:12:33","date_gmt":"2012-06-29T16:12:33","guid":{"rendered":"https:\/\/wprealm.com\/?p=34"},"modified":"2012-06-29T18:12:33","modified_gmt":"2012-06-29T16:12:33","slug":"writing-plugins-is-it-always-worth-it","status":"publish","type":"post","link":"http:\/\/wprealm.local\/writing-plugins-is-it-always-worth-it\/","title":{"rendered":"Writing Plugins: Is It Always Worth It?"},"content":{"rendered":"

Writing code can be boring sometimes, whether you need some new features, but don’t like them or the process itself has somehow become tedious. You cannot focus and you are at a dead-end. The only thing you want is to get this thing working and finish it already. Sometimes what you need to get done\u00a0is so\u00a0simple\u00a0<\/em>that two lines in your theme’s functions.php<\/code> file would probably do the trick, and the code for the header of a plugin alone is 10 lines. Why bother coding a plugin at all?<\/p>\n

<\/p>\n

The usual excuses<\/h3>\n

We all have used any combination of these at any given moment:<\/p>\n

“It’s just the two lines”<\/h4>\n

We develop large plugins or small pieces of code. For the small pieces, we just need to add them to the\u00a0functions.php<\/code> file. Pretty easy, why complicate?<\/p>\n

Consider child themes, for instance. As you know, your\u00a0functions.php<\/code> file belongs to a specific theme, not your whole site. \u00a0If someone updates it, your changes will be lost. If you are the only admin or developer and you have a good memory, you can probably risk it, \u00a0but isn’t it just easier to not have to remember<\/strong> and have a plugin that you can activate and deactivate without having to touch anything else?<\/p>\n

\"\"
Changing the default attachment size. Just this one time? I don’t think so.<\/figcaption><\/figure>\n

“I’m not going to reuse it”<\/h4>\n

The code is for this one specific client only. You’re not going to use it again. Never. Ever. You’re absolutely sure.<\/p>\n

Right. The truth is that if you have a piece of code that is simple and useful, you are going to use it again. If you have spent a lot of time developing it, you’ll be proud of it and will end up using it again in your next project. Take a look at the examples below; they were all originally developed for a single use-case. Then they were transformed into plugins, and reused in many projects.<\/p>\n

\"\"
Originally functions.php lines, all grown-up<\/figcaption><\/figure>\n

“I’m not going to use it in another language, no need to make it translatable”<\/h4>\n

When you are developing for clients, your main concern is their needs. At first, you think to yourself “my client is from Spain, and Spanish people only use stuff in Spanish, why bother?”, “I’ll do it in English only, anyone can use it” or even “I’m going to show off my geekiness, I’ll do it in Klingon”.
\nI myself have some plugins and code whose HTML on the frontend is in Spanish only. Despite it being very common, however, it doesn’t make it right<\/strong>. \u00a0Internationalization<\/a>\u00a0looks difficult\u00a0at first, even tricky. With little time, you will notice that it is actually quite easy and just another step in your development cycle. Your plugin will be translated by many and will gain a visibility that it wouldn’t have if you hard-coded its strings.<\/p>\n

Plugin beats functions.php<\/h3>\n

I hope it has become clear that there are benefits in writing plugins instead of using disordered pieces of code. It’s not alway easy to remember in which project you used a piece of code that you need in the new one. If you have everything neatly packaged, it will be good for everyone \u2013 and great for you.\u00a0Your first couple of plugins will be a bit messy and sometimes difficult to get to work. You’ll need to write clean, nicely indented code and prepare a header for your plugin, but \u00a0the Codex makes it easy to get started<\/a>. You could even use another plugin’s code to start your own.<\/p>\n

Community<\/h4>\n

When you write code, you have a community of developers that can help you. If you are working with php there’s\u00a0php.net<\/a> to solve your questions, for instance, or even Stack Overflow<\/a>. If you share your code in \u00a0public repositories such as\u00a0GitHub<\/a>, people can see it, fork it and help you improve it.<\/p>\n

Keep in mind that you are coding PHP, a programming language that has one of the biggest developer communities. You are coding for WordPress, too, not only for you, but for the whole community. If you need to write a plugin about anything, you will probably find a plugin that can help you get started, in the repository. They share, you share, all under the GPL. This why you are never alone in your corner, churning out code; there are literally thousands of developers ready to help<\/strong>. Really. Just take a look at\u00a0these archives<\/a>.<\/p>\n

When writing code for WordPress, here are your best resources:<\/p>\n