In a previous post, I had shown how to create a WordPress plugin in minutes, which will come in handy now to add snippets and functions to your plugin.

Adding snippets and functions to your plugin is very simple, as long as you have some basic knowledge of PHP. In this post, I will show you how to add a few lines of code to your plugin and keep your code organised.

Before we get started, you will need to have a few things:

  • A text editor like Visual Code Studio.
  • A local development environment like XAMPP, MAMP or Local(The easiest one to get you started).
  • Basic knowledge of PHP.
  • Your custom plugin, if you haven't created one check out how to create one.

Now let's get started!

Create new file

Navigate to your plugin directory(/wp-content/plugins/my-custom-plugin/) and create a new folder called "/includes/" and file called "snippets.php". The name of the file is up to you, but I like to keep my code organised by separating it into different files.

adding php file snippets inside a includes folder

In your new file, add the following line of code:


// Code goes here

This is the basic structure of a PHP file. Now, let's add our first snippet of code.

Adding a Custom Function

A function is a piece of code that can be reused throughout your website. In WordPress, there are already many functions available for you to use, but sometimes you need to create your own custom function. Let's say we want to create a function that displays the current date.


// Code goes here

function custom_display_date() {

return date( 'l, F jS, Y' ); // will output something like "Monday, December 31st, 2021"


add_shortcode( 'current-date', 'custom_display_date' );
adding custom display date inside a plugin

This code does two things. First, it calls the custom_display_date() function we created. Second, it adds a new shortcode called [current-date]m that we can use for example in gutenber to display the current date anywhere on our website.

Note that the output on the front end is [current-date], and the date is not shown, that's because we need to include the includes/snippets.php file inside my-custom-plugin.php file.

Include snippets.php file

To do that, add the following line of code to my-custom-plugin.php:

include( plugin_dir_path( __FILE__ ) . 'includes/snippets.php'); // don't forget to change the path if your file is in a different folder

This code actually loops trough all the files inside the 'includes' folder, which means if you create any other files than 'snippets.php', they will all be included from that folder.


Now if you go back to the front end of your website, you should see the current date being displayed.

Shortcodes are a great way to add content to your posts and pages without having to write any code. WordPress comes with many shortcodes built in, but sometimes you need to create your own custom shortcode to call a function you have created or a code snippet you have added.