Admittedly this is probably one of my most favorite pieces of code, but it has always had one detracting piece … it is very performance heavy. So, in an effort to reduce this performance hit I have implemented a WordPress transient set with a one (1) month refresh. I have also made some other modifications since the last posting of this code and will be sharing what will be used in the next release of the Opus Primus WordPress theme.

Adding a transient to the function is relatively straight forward once you get your head wrapped around what you need to use it for. In this case, the biggest hit on performance is still a call to get_posts() to find the $first_post but this is now offset but setting the $first_post value as a transient. This transient is then set to refresh after one (1) month as it will rarely change so an extended period should be fine (although a parameter exists to change this value).

To see this version of the dynamic copyright method in action just scroll to the bottom of the page … and also note how the copyright changes depending on whether it is a “list” view or a “single” view that you are reading. You can view the code in its current context here, or see the code below.

To see the code click here.To hide the code click here.

	/**
	 * Copyright
	 * Returns copyright year(s) as defined by the dates found in published
	 * posts. Recognized the site (via its title) as the copyright holder and
	 * notes the terms of the copyright. By default the author of the page or
	 * the post is specifically noted as the copyright holder in the single
	 * view of the page or post.
	 *
	 * @package     OpusPrimus
	 * @since       0.1
	 *
	 * @internal    $output can be filtered via the `opus_copyright` hook
	 * @internal    $transient_refresh is set to one month (2592000s = 30 days)
	 * for $first_post as a default since it will rarely, if ever, change
	 *
	 * @uses        __
	 * @uses        apply_filters
	 * @uses        esc_attr
	 * @uses        get_bloginfo
	 * @uses        get_posts
	 * @uses        get_transient
	 * @uses        home_url
	 * @uses        post_date_gmt
	 * @uses        set_transient
	 *
	 * @param   bool $show
	 * @param   bool $by_author
	 * @param    int $transient_refresh
	 *
	 * @return  mixed|null|void
	 *
	 * @version     1.2.4
	 * @date        May 18, 2014
	 * Used transients to improve performance impact of the method
	 */
	function copyright( $show = true, $by_author = true, $transient_refresh = 2592000 ) {

		/** If we are not going to show the copyright jump out now */
		if ( false == $show ) {
			return null;
		}
		/** End if - show */

		/** @var $output - initialize output variable to empty */
		$output = '';

		/** Take some of the load off with a transient of the first post */
		if ( ! get_transient( 'opus_primus_copyright_first_post' ) ) {

			/** @var $all_posts - retrieve all published posts in ascending order */
			$all_posts = get_posts( 'post_status=publish&order=ASC' );
			/** @var $first_post - get the first post */
			$first_post = $all_posts[0];

			/** Set the transient (default: one month) */
			set_transient( 'opus_primus_copyright_first_post', $first_post, $transient_refresh );
		}

		/** @var $first_post_date - get the date in a standardized format */
		$first_post_date = get_transient( 'opus_primus_copyright_first_post' )->post_date_gmt;

		/** First post year versus current year */
		$first_post_year = substr( $first_post_date, 0, 4 );
		if ( $first_post_year == '' ) {
			$first_post_year = date( 'Y' );
		}
		/** End if - first post year */

		/** Add to output string */
		if ( $first_post_year == date( 'Y' ) ) {
			/** Only use current year if no published posts in previous years */
			$output .= sprintf( __( 'Copyright © %1$s', 'opus-primus' ), date( 'Y' ) );
		} else {
			$output .= sprintf( __( 'Copyright © %1$s-%2$s', 'opus-primus' ), $first_post_year, date( 'Y' ) );
		}
		/** End if - first post year */

		/**
		 * Append content owner.
		 * Default settings will show post author as the copyright holder in
		 * single and page views.
		 */
		if ( ( is_single() || is_page() ) && $by_author ) {
			global $post;
			$author     = get_the_author_meta( 'display_name', $post->post_author );
			$author_url = get_the_author_meta( 'user_url', $post->post_author );
			$output .= ' <a href="' . $author_url . '" title="' . esc_attr( sprintf( __( 'To the web site of %1$s', 'opus-primus' ), $author ) ) . '" rel="author">' . $author . '</a>';
		} else {
			$output .= ' <a href="' . home_url( '/' ) . '" title="' . esc_attr( get_bloginfo( 'name', 'display' ) ) . '" rel="home">' . get_bloginfo( 'name', 'display' ) . '</a>';
		}
		/** End if - is single */

		/** Append usage terms */
		$output .= ' ' . __( 'All Rights Reserved.', 'opus-primus' );

		return apply_filters( 'opus_copyright', $output );

	}

	/** End function - copyright */

In the case of the code above, you may note the use of transients at lines 048 through 060. This code can be applied to previous versions of the dynamic copyright code that can be found on this site, but I will leave that as an exercise for the reader.

You can also just copy and paste this code into you own theme if you see a use for it. Please be my guest, the code falls under the same license as Opus Primus, but please also make the necessary changes to the text domain, name prefixes, etc. so that the code matches your theme’s choices.

Enjoy!