14 July 2022

How To Add WordPress Code Snippets The Easy Way.

By Rahul Garg

I’m a fan of using WordPress plugins because they make my life easier. I sometimes end up with the situation though where I’ve overloaded my blog. Then, I have to take drastic action.

More than likely there are WordPress code snippets I could use instead. That would remove the need for certain plugins.

Still, messing around with my blog’s inner workings isn’t something I take for granted.

Up until recently I had the Dynamik Website Builder installed. That made adding WordPress code snippets easy.

But since I had a conflict between a plugin and that child theme, I moved to a StudioPress one instead. I’ve now become encamped in the Altitude Pro theme.

I found myself looking for an easy way to add WordPress code snippets. Without updating the child theme files was what I was looking for.

FINDING A SOLUTION

Several plugins on the WordPress repository seemed promising. But all the ones I sourced didn’t quite fit what I was looking for.

After searching on Google, I found an article on WPBeginner about site-specific wordpress plugins.

It’s not something I’d thought about before but it makes sense for several reasons:

  1. You don’t have to update your WordPress core theme files;
  2. Your code snippets are portable between different themes;
  3. You won’t lose your php codes should your theme get updated;
  4. You can combine all the code snippets in one place, and;
  5. You might be able to get rid of some other plugins in the process.

An extra benefit is that if something goes wrong, WordPress will deactivate your plugin.

Imagine if the code you found on the web had a typo in it and you added it to your functions.php file. More than likely you’d get a white screen and be unable to access your site.

Using a site-specific plugin, on logging back into WordPress, you’d regain access.

WHAT THE HECK IS A SITE-SPECIFIC WORDPRESS PLUGIN?

As WPBeginner explains, it’s a place for you to add code snippets you find around the web.

You should still make any theme-specific code changes to the functions.php file. If you changed themes, some of the codes in your plugin might not be compatible. So, it’s best not to include those.

At the same time, if you’re using a framework such as Genesis, you might still consider it. Because you’re using a child theme, changing it shouldn’t cause incompatibility.

So you could add Genesis code snippets into a site-specific WordPress plugin. If you then move to a different theme or framework you would need to remove any Genesis code.

.

SETTING UP YOUR OWN PLUGIN FOR WORDPRESS CODE SNIPPETS

It might sound a bit daunting but setting up your own plugin isn’t too bad. Did I say easy? 

You’ll need to access your site via ftp or through your hosting account. A couple of free tools I use for ftp access are Filezilla and Cyberduck.

Your web host can give you your login details for ftp access to your site.

There’s a few things you need to do once you’re in your site’s directory:

  • Create a new folder in the plugins directory. Example: /wp-content/plugins/yoursitename-plugin/
  • Open a blank file using notepad and save it as “yoursitename-plugin.php”
  • Edit the file and add the following code:

<?php

/*

Plugin Name: Site Plugin for yoursitename.com

Description: Site specific code changes for yoursitename.com

*/

/* Start Adding Functions Below this Line */

/* Stop Adding Functions Above this Line */

?>

  • Save the file and then upload it to the folder you created in your plugins directory.
  • Then go to your WordPress Dashboard and activate the plugin.

DO YOU USE WORDPRESS CODE SNIPPETS?

Some people don’t like to tinker and prefer to use a ready made plugin solution. Others are happy to get their hands dirty and customise their blogs with PHP code.

Which one are you? Do you use plugins for everything or update your theme files? What do you think of using a site-specific plugin?

If you fancy getting yourself a site-specific plugin and need help, give me a shout!

Please follow and like us:
Pin Share