A simple tutorial on how to write your first shortcode for WordPress.
It is a common practice, to add a short copyright notice on the bottom of the website. Usually, it also includes the year. Here is a standard example of such notice:
In the time of writing, it is the year 2018. But when it will be the year 2019, we want it to update automatically.
In this short tutorial, we will create a shortcode, which we will use in the copyright footer area to update the year automatically.
Here is what we’ll learn:
- How to use a child theme for customization
- How to create a simple WordPress shortcode
- How to use PHP date function.
TL;DR
functions.php file:
<?php /* this goes in functions.php file in your child theme */</code> function current_year_shortcode_function() { $year = date('Y'); return $year; } add_shortcode('year', 'current_year_shortcode_function');
Usage within WordPress:
/* usage */ /* On the front page you can use it by calling the newly created "year" shortcode in square brackets: */ 2024 © Copyright, OrionThemes. All Rights Reserved.
How (and why) to use a child theme for customization
A child theme is a great way to add new or customize any theme functionality and keep your (main) theme upgrade friendly.
It is a common novice web developer mistake to modify the theme’s PHP, CSS or JS files directly. It is easy to modify existing code, but there is a drawback to this. When a theme update comes with new features and bugfixes, you will have to make a decision. Either to skip the upgrade or, upgrade the theme and add all patches and functionality changes again. However, if modifications are added into the child theme, you can easily upgrade your main theme and still keep your own functionalities.
That is why we include a sample child theme with each of our themes on ThemeForest, to make the customization as simple as we can for our clients.
You can add any php functions in the functions.php file in the child theme and all CSS modifications into style.css file.
How to create a simple WordPress shortcode
WordPress comes with a nifty add_shortcode() function, which kind of does everything for us, so we just need to write the functionality we want. The function accepts two arguments. The first one is the shortcode tag and the second is a name of the function which we want to call with the tag:
<?php add_shortcode( 'shortcode_tag' , 'shortcode_function_name' ); ?>
Whenever a shortcode tag is used within the WordPress content, the shortcode function will load and return the result, which will display on the front end.
For the purpose of displaying a current year, we will use the ‘year’ shortcode tag and a ‘current_year_shortcode_function’ function name:
add_shortcode('year', 'current_year_shortcode_function');
Now we just need the PHP function which returns the current year:
How to use PHP date function
So we want our function to return the current year. For this purpose, we will use the date() function.
function current_year_shortcode_function() { $year = date('Y'); return $year; }
Conclusion
We used an add_shortcode function to create a WordPress shortcode and a custom function which returns the current year.
The final code which needs to be added to functions.php file in your child theme is
function current_year_shortcode_function() { $year = date('Y'); return $year; } add_shortcode('year', 'current_year_shortcode_function');
And you can use it anywhere in the content simply by adding “year” in square brackets.
2024
Happy coding!