• Facebook Icon Link
    • Instagram Icon Link

    How to Display the Current Year in WordPress With a Shortcode.


    display the current year with a shortcode

    Overview

    Most websites have the copyright and year displayed in the footer. When I come across a website with an old year, it makes me question if the information on the website is out of date. This tutorial will explain how to allow the use of shortcodes in your widgets and then how to create the shortcode that will dynamically display the current year in your footer.

    Quickies.

    What is a WordPress Shortcode?

    A shortcode is a bit of text between two square brackets that when added to a post, page or widget will execute a piece of PHP code. In-depth tutorial here: How to Make a Custom Shortcode in WordPress.

    How to Allow the Use of Shortcodes in WordPress Widgets and Footer.

    By default, WordPress does not support shortcode use in widgets. There is a simple way to get around this by adding a single line of code to your functions.php file. If you are editing the functions.php file, make sure you are using a child theme. More info on child themes: How to Make a WordPress Child Theme. There are a couple of ways to edit the functions.php file, you can make an FTP connection or do it directly in the WordPress backend. If you want to use FTP, see these instructions: How to Make an FTP Connection with FileZilla.

    How to Edit the functions.php File in the WordPress Backend.

    1. Login to the WordPress website admin area.
    2. In the left sidebar go to Appearance > Theme Editor.

    WordPress Theme Editor

    1. In the upper right hand corner, make sure you are editing the child theme, not the main theme.
    2. In the right sidebar select the functions.php file.

    Edit WordPress functions.php file

    1. Add the following piece of code to the file.
    add_filter ('widget_text', 'do_shortcode');
    1. Save or Update the file.

    Now you can use WordPress shortcodes in your widgets.

    Divi WordPress Theme Banner

    How to Display the Current Year Dynamically with a Shortcode.

    In order to write our shortcode, we need to access the functions.php file. As stated above, there are a couple of ways to edit the functions.php file, you can make an FTP connection or do it directly in the WordPress backend. If you want to use FTP, see these instructions: How to Make an FTP Connection with FileZilla.

    How to Edit the functions.php File in the WordPress Backend.

    1. Login to the WordPress website admin area.
    2. In the left sidebar go to Appearance > Theme Editor.

    WordPress Theme Editor

    1. In the upper right hand corner, make sure you are editing the child theme, not the main theme.
    2. In the right sidebar select the functions.php file.

    Edit WordPress functions.php file

    1. Add the following piece of code to the file.
    function display_year() {
        $year = date('Y');
        return $year;
    }
    add_shortcode('year', 'display_year');
    1. Save or Update the file.

    Now you can use the shortcode [year] in your WordPress widgets.

     

    Additional Posts.

    Pin It on Pinterest

    Share This