A real-life example of when you should write your own code instead of relying on a plugin

One of the greatest things about WordPress is how easy it is to (sometimes dramatically) customize the platform–from themes to plugins that add complex new functionality or fundamentally alter how the original functionality was intended to work. There is no doubt in my mind that in 2019 onward, Advanced Custom Fields is a must-have plugin for any WordPress site and there is truly no substitution for WooCommerce when an ecommerce solution is required. But this ease of customization through thousands of free-to-use plugins can make it very easy for laziness to take hold and here is a real-life example of when you should write your own code instead of relying on a plugin.

Plugins can be a huge boon to a project with a tight timeline or to provide a feature that would otherwise be figuratively impossible to implement from scratch (i.e. custom ecommerce solution) but they can also overcomplicate otherwise simple things, resulting in a website bloated with many instances of jQuery being loaded (often always different versions as well) and dependencies on someone else’s code and time/ability to keep their code updated.

For simple things, it’s almost certainly a better idea to get your hands dirty and write some PHP to accomplish what you need. This way, you have complete control over the code, behavior, and aesthetic. And if you’re not all that comfortable writing PHP or working with code, it’s great practice to familiarize yourself with conventions of not only PHP but WordPress.

As an example of something that should be hand-rolled, I’ll show how simple and quick it is to build your own “social share” component. This isn’t an auto-poster (something that automatically posts on your behalf to Facebook, Instagram, Twitter, etc. when a new post is published) but rather a simple grouping of social network icons linked to their respective sharing functionality so a user can quickly share your content with their friends and followers. There are multiple plugins available that add this functionality and do so in a way that requires little to no involvement from the site owner/developer aside from installing and enabling the plugin and adjusting a few settings, but adds overhead to your site and removes yet another aspect of the site from your direct control.

The following assumptions are being made:
You have access to your theme files
You have a `template-parts/` directory in your theme
You are using Font Awesome (version 4.7)

Create a new file called social-share.php inside the template-parts/ directory. Open the file in your code editor. Determine the social networks you want to allow users to share to and figure out which icon style you want to use (Font Awesome provides multiple versions of brand logos for varying style needs) and then do some quick searching to ensure you have the correct URL that needs to be opened by users in order to share your post with their friends and followers.

  • For Facebook, use: https://www.facebook.com/sharer/sharer.php?u=
  • For LinkedIn use: https://www.linkedin.com/shareArticle?mini=true&url=
  • For Twitter use: https://twitter.com/intent/tweet?url=

In each of those URLs, we need to supply the post’s permalink after the equals sign ending each URL.

And now, simply build the basic HTML structure that will be output, including a container div and descriptive label.

<div class="social-share">
<span>Share: </span>
<a href="https://twitter.com/intent/tweet?url=<?php the_permalink();?>" target="_blank"><i class="fa fa-twitter-square"></i></a>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink();?>" target="_blank"><i class="fa fa-facebook-square"></i></a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink();?>" target="_blank"><i class="fa fa-linkedin-square"></i></a>
</div>

And that’s it; no need for a third-party plugin to accomplish this! It’s got simple structure that can be easily modified via CSS and updated as additional social media platforms emerge.

To use this, simply call:
<?php get_template_part( 'template-parts/social-share' ); ?>

anywhere you want those links to appear on your site and WordPress will handle the rest.

Comments