Most of the time when you see a little snippet of WordPress code somewhere on the internet, you’re told to just put it in your theme’s functions.php file.
Problem is, when you update your theme, you’ll lose that code. It also gets pretty messy to have a bunch of random code building into your functions.php file even if you have created a child theme.
Instead you can create a small “functional WordPress plugin” to add that functionality.
Create a new PHP file in wp-content/plugins/
Create a new file in your wp-content/plugins/ folder called something that describes what you’re doing, with a .php at the end. Maybe add-field-to-post.php or use-meta-in-other-plugin.php
Add the standard plugin header
Just copy and paste the following header and put it at the top of your .php file. Edit the plugin description, author (to you) to better describe what this snippet of code is doing.
<?php /* Plugin Name: Customize Other Plugin Output Plugin URI: http://brianhogg.com/ Description: Changes the output from The Other Plugin using a bit of code Author: Brian Hogg Version: 1.0.0 Author URI: http://brianhogg.com License: GPL2 */
Add your code underneath
Now just add in the code you were going to copy/paste into your functions.php file below this header. Avoid putting a closing ?> PHP tag at the end to avoid any errors with extra whitespace, which would likely break your theme, RSS feeds, and a bunch of other things.
Activate the plugin
Now just go to Plugins in your WordPress dashboard, and activate that plugin.
Voila! You’ve now created a small functional WordPress plugin that clearly describes what it does, and will not be removed when you update your theme. You can also easily deactivate or delete the plugin if you no longer need that piece of code.