• Resolved itmonitor

    (@itmonitor)


    Hello,

    What I did: I added the below code to my WordPress pages (Text mode), so the current year will be displayed, and automatically updated each year. The code works, in Text mode I click the button Update and the copyright notice is nicely displayed in the website, with the current year in it.
    —-
    Issue: However, when I change the page from Text mode into Visual mode, WordPress will automatically change the code pasted into the page. When I get back to Text mode, the code is corrupted. This means that everytime I update the text in a page, the copyright code at the end of the text will be corrupted by WordPress.
    —–
    Question: Is there any way to improve avoid the code below, so it will not change by WordPress, when I change the page from Text mode into Visual mode in the Dashboard? Many thanks.

    &copy; <?php
    $copyYear = 2007; // Set your website start date
    $curYear = date('Y'); // Keeps the second year updated
    echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
    ?> Copyright, all rights reserved <a title="MyWebsite" href="http://mydomain.com/">MyWebsite</a> | <a title="Contact" href="https://mydomain.com/?page_id=259">Contact</a>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Consider making a custom shortcode. Your page text will be like this:

    &copy; [my-shortcode] Copyright...

    “[my-shortcode]” will not be affected by switching editor tabs.

    Next, make a child theme. This is so that your shortcode definition will not be overwritten when you update your theme.

    In the root of your child theme, make a file called functions.php to define what the shortcode should do:

    <?php
    function my-shortcode() {
      $copyYear = 2007; // Set your website start date
      $curYear = date('Y'); // Keeps the second year updated
      return $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
    }
    add_shortcode('my-shortcode', 'my-shortcode');

    Thread Starter itmonitor

    (@itmonitor)

    Hi Iorro, many thanks!

    Is there any way not to use a child theme for this…perhaps if I create a custom-functions.php and adding the code you propose, I know that the custom-functions.php file will not be overwritten on theme updates. But I do not know if your code will work! 🙂

    Sort of, if you make a file called custom-functions.php in your main theme, it will not be overwritten on theme updates. But you need to link to it, so you will need a single line at the end of functions.php

    include-once('custom-functions.php');

    so you are back to square one because functions.php will be overwritten.
    Go for a child theme. Its far less painless than it appears to be, has other advantages and is the recommended way of customising.

    If the code doesn’t work your site may crash. Using FTP or your file manager software, just rename the directory:
    wp-content/themes/my-child-theme
    and you should be back in business.

    Thread Starter itmonitor

    (@itmonitor)

    Iorro, many thanks. I will do as you instruct.

    Thread Starter itmonitor

    (@itmonitor)

    .

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Adding dynamic copyright year into pages’ is closed to new replies.