[ad_1]

People are building WordPress sites every day.

Some take the out-of-the-box approach. That is, they install WordPress, configure it, and use a theme exactly as it was created. They'll start writing blog posts and that's it.

Others like to personalize things a bit. And while you can accomplish a lot by using the right theme and specific plugins, there may come a time when you will find that you have to dig deeper into the code yourself and add a few lines on your own.

But how should we proceed? This is the question I want to explore here today.

It depends

Yes, this two word sentence is the short answer to this question.

But that wouldn't make a very exciting article, would it? Yet it is the truth. The best way to add custom code to your WordPress site depends on a variety of factors. This is why I have chosen to present your options in an “if / then” format.

If you have installed a theme created by someone else ...

In that case, you shouldn't add custom code to the files for this theme, says Erich Boileau, owner of Gaijin LLC, a web design company.

“As soon as you update the kernel, plugin or theme, your changes will be erased,” he says. It would be a huge waste of time, don't you think?

So if you can't edit the code directly in the theme files, what should you do?

Create a child theme

According to Brandon Howard, owner of All My Web Needs, a WordPress web design company, creating a child theme allows you to add code to the fonctions.php file without worrying about losing your edits once you download WordPress Core or theme updates.

Additionally, child themes allow you "to use WordPress and theme-specific hooks […] to insert your code," he says. You will never run the risk of “overwriting” your changes again.

fonctions.php
functions.php: fair game with a child theme installed.

In case you are not clear on what a child theme is, let me explain. This is basically a subtheme of your installed main theme. It is created in a very simple way. Basically all you have to do is access your site via FTP and create a folder (something like child-theme-name will work) then create a style.css file that corresponds to this folder. This CSS file will extract the information from the CSS file of the parent theme. Then you just need to activate the child theme in your dashboard.

I won't go into a ton of details on how to do this. We actually covered how to make your own child theme in depth a while back, so you might want to check this out if you decide that's how you want to add custom code to your site. Of course, this option may not work for everyone.

Use a plugin

If you are not used to working with the backend of WordPress but still want to make custom code changes, you can use a plugin that allows you to insert code in strategic places on your site. According to Scott Chow of The Blog Starter, using a plugin "greatly reduces the chances of something being accidentally deleted." Not to mention that it eliminates the whole “update your site and lose your changes” problem.

He recommends two plugins, in particular. The first is to insert headers and footers. This plugin allows you to insert code in the header or footer of your site. It's perfect for people who are trying to set up something like Google Analytics, says Chow.

The second plugin he recommends is Global Content Blocks. This plugin allows you to insert code into posts and save those snippets for later use.

Global content blocks
Global Content Blocks: a very popular plugin.

Another developer I spoke with, Natalia Chriss, has an additional plugin recommendation. It's called Code Insert Manager and it's great for adding code for services like Google Analytics, Aweber, and GetResponse.

But then again, there might not be a plugin that can accomplish the specific coding task you set out to complete. For these cases, you can ...

Create a plugin

When you want to redesign a site while maintaining the same functionality - or if you just want to add code somewhere on your site - you can create a plugin to manage it.

"I am creating a site specific functionality plugin for the custom code instead of putting it in the theme fonctions.php file, ”says Adam Smith, IT director at Marketing Mojo. “This separates how WordPress works from design,” he says, indicating that this most often applies when using someone else's theme rather than one you created from scratch.

In this custom functionality plugin, Smith usually includes custom post types, shortcodes, WordPress admin customizations, and other changes he deems necessary.

Howard takes a similar approach. “There are a lot of cases where we build custom plugins with only 10-20 lines of code just because we want to add a little functionality and it's important that the functionality remains in place even if the theme is changed,” he says. he.

If you have created your own theme ...

So far, we've just been talking about what to do if you want to add custom code to a theme you've downloaded for free or purchased from a theme marketplace. But what if you created the theme yourself? What are the best practices for these situations?

If you've written your theme from scratch, "you can insert the code directly into the theme," says Boileau, which would have the side effect of improving the speed and efficiency of the site. Just tap your theme fonctions.php file whenever you want to make a small change should be enough.

Create a plugin (yes, again)

Yes, this method applies to those who use other people's themes as well as to those who create them themselves.

There are some great advantages to making a plugin to accompany your own theme. Not only does this allow you to turn specific site features on and off if you decide you no longer need them, but it also gives you the option of getting started with plugin development. If you think the plugin code you create is useful to other developers, "you can consider writing your own brand new plugin and submitting it to WordPress to help others," says Boileau.

So what is the best solution?

Honestly, it depends. Almost every developer I've spoken to has said so. Still, that doesn't mean that there aren't some proven rules. When prompted to add custom code to a WordPress site, you should:

  • Use a child theme when modifying functions on someone else's theme, or
  • Use a plugin to add blocks of code to your site, or
  • Create plugins to add wider side features.

This strategy should help you create custom sites that stay exactly the way you want them to, regardless of new updates rolled out.

Conclusion

And There you go! Hopefully, you now have a better understanding of how to add custom code to your WordPress site and which method is best given your current situation.

But what I want to know is if I missed something? Have you found a different method to be more efficient? Want to add to a method I already mentioned? Either way, feel free to share and join the discussion below!

Photo credit: Tuts +

[ad_2]

Source link

0 shares
Share
tweet
Save