Dear theme developers, please stop pasting random code snippets into functions

TG Database is a platform for organized data management.
Post Reply
Nihan009
Posts: 6
Joined: Sun Dec 22, 2024 3:35 am

Dear theme developers, please stop pasting random code snippets into functions

Post by Nihan009 »

Last updateJanuary 26th, 2024by Editorial Team
FacebookTweetLinkedInPinShares0
Imagine this scenario, you find a really cool code snippet on one of the many WordPress tutorial sites out there and you paste it into your theme's functions.php file.

The code snippet works as advertised and you put your theme up for sale on a well-known theme marketplace. We’re going to pick one at random out of a hat and go with… ThemeForest.

Suddenly your theme becomes very popular, it may be because of the huge list of seemingly useful “features” you have listed on your theme’s sales page. With the success of your theme also comes a number of support queries, mostly related to plugins that break when using your theme.

How did this happen? Maybe because you blindly pasted random bits of WordPress code into your functions.php file without thinking or anticipating possible compatibility issues.


A real life example
So, I was trying to find a piece of code that would pull all the attached images from a post and then display them in that post automatically. I finally found a piece of code on Stack Overflow , How to Make a Bio Dropdown Box with CSS
pasted it into my functions file, and it seemed to solve the problem.

The first line of code was as follows:

add_filter('the_content', 'strip_shortcodes');
Well, it worked, I didn't think anything of it. Later I tried to embed a contact form with a shortcode. Surprise, it didn't work and I spent an hour trying to figure out why. If I had read the code I was pasting, I would have known.

This was for a client site, not a released theme, so luckily I didn't have russian virtual mobile number to deal with a deluge of support queries due to my stupid mistake .

What commercial plugin developers think
Here's a quote from Carl Hancock (developer of Gravity Forms) on this very topic:

Supporting the popular Gravity Forms plugin means we see more than our fair share of poorly coded themes. One of the main support-related issues we encounter is themes that are not developed using best practices, resulting in Gravity Forms styling issues and in some cases conflicts that result in Gravity Forms not working properly.

The biggest culprit of these situations are themes that include code snippets copied and pasted from tutorial sites. Theme developers seem to think that just because the code snippet was on a tutorial site, it must be good. Unfortunately, this is not always the case and these poor decisions lead to headaches and support issues for users.

Do you want to limit the chance of plugin issues caused by a poorly developed theme? Stick to reputable theme developers, such as Press75, iThemes, Headway Themes, Organic Themes, WooThemes, and StudioPress, to name a few. Be wary of theme marketplaces where the author's experience and skills may be scarce. In most cases, you get what you pay for.

Good coding practices
Many of these issues can be avoided by following WordPress coding standards . For example, you should prefix your function names to avoid potential conflicts.

Image


For styling issues with Gravity Forms, you may want to avoid certain general styles on <form> el formularioand <id> elements entrada, and instead use the default WordPress ID selectors for most of your form styling.

These include #searchform, #s, #searchsubmitin the search box. Also #commentform #author, #url, #email, #comment, #submitfor the feedback form.

Conclusion
If you are a theme developer, and not very well versed in PHP, be careful when copying and pasting these code snippets into your theme. Even if you are not very good at PHP, you can at least read the code and try to understand it before using it.

For example, if you find that your shortcodes aren’t working properly, a line of code that mentions “strip_shortcodes” could have something to do with that.

Sometimes I get the feeling that WordPress theme developers just paste random snippets into their functions.php file, just so they can list another “feature” on their theme sales pages.

While I'm not a huge fan of this kind of idea, it does enter into another discussion about the role of themes and plugins in WordPress sites, which I'll save for a future post.
Post Reply