When adding functionality to your WordPress website you would usually add a pre-made plugin, usually for the WordPress Repository if you can find what you need. You may find a piece of code you want to add to your sites function.php file in your themes file directory, but if you want to change your theme later down the road you will have to transfer all the code for one theme’s function.php file to another if you remember!
If the code you want to add is to change or added something that is not theme dependant you should consider adding the code to a new plugin so it stays active if you decide to change themes. I myself have recently changed themes for a few of the sites I manage including this one and it was quite a lot of work.
Its surprisingly easy to make your own plugin details follow 🙂
The first step would be to add a new folder in the plugin directory on your website host /root-of-hosting/website-url/public_html/wp-content/plugins/ you can call the folder what ever you like as long as it won’t conflict with another plugin. for example, I used /simple-years-since-date-shortcode/ for a plugin I made on this website.
Then you make a new php file inside the folder again I used simple-years-since-date-shortcode.php
Then your ready to add the code to the php file.
<?php
/**
* Plugin Name: Simple Years Since Date Shortcode
* Plugin URI: https://marknhewitt.co.uk/wordpress/plugins/simple-years-since-date-shortcode/
* Description: A Simple shortcode to display the years since a given date. Example : [show-years-since ysdate="04/29/1979"] will output the age for that birthday date.
* Version: 1.1
* Requires at least: 3.5
* Requires PHP: 7.2
* Author: Mark Hewitt
* Author URI: https://marknhewitt.co.uk/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: simple-years-since-date-shortcode
*/
function years_since_date($ysdate) {
extract( shortcode_atts( array(
'ysdate' => 'ysdate'
), $ysdate));
return date("Y", time() - strtotime($ysdate)) - 1970;
}
add_shortcode('show-years-since', 'years_since_date');
The only important things to include are the [* Plugin Name: and the * Text Domain:] and of course the Function code. These give the plugin title and the plugin name. everything else is only important if you are sharing your plugin.
Download the plugin I used as an example HERE