How to Implement a Dynamic Copyright Year in Your WordPress Footer
In this tutorial, we will learn how to implement a dynamic copyright year in the footer of your WordPress website. By dynamically updating the copyright year, you can ensure that your website always displays the current year automatically. We will provide two methods: one using a hook and the other using a shortcode. Both methods will avoid the use of plugins.
Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.
Method 1: Using a Hook
Add a Custom Copyright Function
To get started, locate and open the functions.php
file of your WordPress child theme. Inside the functions.php
file, add the following code snippet:
function my_custom_copyright() {
$current_year = date( 'Y' );
echo '© ' . $current_year . ' Your Website Name. All rights reserved.';
}
Code language: PHP (php)
Explanation:
- We define a function named
my_custom_copyright
. - The
date('Y')
function retrieves the current year using the ‘Y’ format. - We concatenate the current year with your website name and the copyright text.
- Finally, we use the
echo
statement to output the copyright text.
Hook the Custom Function to the Footer
To display the copyright text in the footer, we need to hook our custom function to the appropriate WordPress action. Add the following code snippet below the previous one:
function my_custom_copyright_hook() {
add_action( 'wp_footer', 'my_custom_copyright' );
}
add_action( 'after_setup_theme', 'my_custom_copyright_hook' );
Code language: PHP (php)
Explanation:
- We define a new function called
my_custom_copyright_hook
. - Inside this function, we use the
add_action
function to hook our custom copyright function to thewp_footer
action. - By using the
after_setup_theme
action, we ensure that our copyright function is hooked correctly.
Method 2: Using a Shortcode
Add a Custom Shortcode Function
Inside the functions.php file of your child theme, add the following code snippet:
function my_custom_copyright_shortcode() {
$current_year = date( 'Y' );
return '© ' . $current_year . ' Your Website Name. All rights reserved.';
}
add_shortcode( 'copyright', 'my_custom_copyright_shortcode' );
Code language: PHP (php)
Explanation:
- We define a function named
my_custom_copyright_shortcode
. - The
date('Y')
function retrieves the current year using the ‘Y’ format. - We concatenate the current year with your website name and the copyright text.
- We use the
add_shortcode
function to register the shortcodecopyright
and associate it with our custom shortcode function.
Implement the Shortcode in the Footer
Via footer.php
Open your footer.php file, typically located in your child theme’s template directory, and find the section where you want to display the copyright text. Add the following shortcode:
echo do_shortcode( '[copyright]' );
Code language: PHP (php)
Explanation:
- The
do_shortcode
function allows us to execute shortcodes in PHP. - By using the
copyright
shortcode within thedo_shortcode
function, we display the copyright text generated by our custom shortcode function.
Via shortcode in Text Widget
Go to your WordPress admin dashboard and navigate to “Appearance” > “Widgets“. Find the footer widget area where you want to display the copyright text and add a “Text” widget. In the “Text” widget, insert the following shortcode in the content area:
[copyright]
Code language: PHP (php)
Explanation:
- By inserting the
[copyright]
shortcode in the Text widget, we display the copyright text generated by our custom shortcode function.
Save and Upload the Modified Files and Verify the Dynamic Copyright Year
Save the modified functions.php file (for both methods) and, if applicable, save the modified footer.php file (method 1) or the Text widget (method 2). Upload the necessary files to your WordPress child theme directory.
Visit your WordPress website and scroll to the bottom of any page. You should now see the copyright text displayed in the format “© {current_year} Your Website Name. All rights reserved.
” The {current_year}
will be dynamically updated to the current year both in the footer via footer.php and in the footer widget.
If you encounter any issues using the shortcode method in a footer widget, it may be because shortcodes are not enabled in WordPress menus and widgets. To enable shortcodes in menus and widgets, you can refer to our tutorial on How to Enable Shortcodes in WordPress Menus and Widgets. This tutorial will provide step-by-step instructions on enabling shortcodes, ensuring that you can use them in your footer widget effectively.
That’s it! You have successfully implemented a dynamic copyright year in your WordPress footer using both the footer.php and footer widget methods. Now your website will automatically display the correct year each year. Remember to use a child theme and follow WordPress coding standards to maintain your modifications.
Leave your feedback and help us improve 🐶
We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.
Your thoughts matter, leave a reply 💬