How to Add Dynamic Copyright Text in WordPress

WPTurbo » Snippets » How to Add Dynamic Copyright Text in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of manually updating your website’s copyright text every year? Wouldn’t it be great if it automatically displayed the current year without you having to lift a finger? In this article, we’ll show you how to add dynamic copyright text to your WordPress website. With just a few simple steps, you can save time and ensure that your copyright information is always up to date. Stay tuned to discover this time-saving trick!

					function wpturbo_dynamic_copyright_text() {
    $start_year = 2020; // change the year to the starting year of your website
    $current_year = date('Y');
    if ($start_year == $current_year) {
        $copyright_text = $start_year;
    } else {
        $copyright_text = $start_year . ' - ' . $current_year;
    }
    return $copyright_text;
}
add_shortcode( 'wpturbo_copyright', 'wpturbo_dynamic_copyright_text' );
				

The code snippet above provides a solution for creating dynamic copyright text in WordPress. The purpose of this code is to automatically generate the appropriate copyright year(s) for your website.

The first step in the code is to define a function named wpturbo_dynamic_copyright_text(). This function will calculate and return the copyright text based on the starting year of the website and the current year.

Within the function, we assign the starting year of the website to a variable called $start_year (in this example, it is set to 2020). We also obtain the current year using PHP’s date function, specifically the 'Y' format parameter, and assign it to the variable $current_year.

Next, we proceed with an conditional statement (if-else) to determine how the copyright should be displayed. If the starting year and current year are the same, we simply set the $copyright_text variable to the $start_year.

However, if the starting year and current year are different, we concatenate the starting year and current year together with a hyphen (-) symbol in between, and assign the resulting string to the $copyright_text variable.

Finally, we use the return statement to ensure that the calculated copyright text is returned whenever the function is called. This way, we can use the value of wpturbo_dynamic_copyright_text() as dynamic copyright text in various parts of our website.

To make the copyright text easily usable, the code also includes a WordPress shortcode. The add_shortcode() function is used to create a new shortcode named ‘wpturbo_copyright’. This shortcode is then associated with the wpturbo_dynamic_copyright_text() function, allowing us to easily display the copyright text within post content, widgets, or anywhere that supports shortcodes in WordPress.

By inserting the shortcode [wpturbo_copyright] in any post or page, WordPress will automatically replace the shortcode with the generated copyright text based on the logic in the wpturbo_dynamic_copyright_text() function. This ensures that the copyright year(s) are always up-to-date without manual intervention.

Register an account to save your snippets or go Pro to get more features.