I hadn’t really thought about this before as I’d just assumed that any code that I posted would be freely available for anyone else to use as they saw fit but it turns out that I actually implicitly retain the copyright if no license is provided. I have now added a Creative Commons Attribution 4.0 International License for all posts covering the code within but in the course of doing so I’ve learned a bit more about how WordPress sites work and that there doesn’t seem to be a simple solution for licensing the contents of blog posts as opposed to just licensing the entire site contents.

License

I decided to apply a Creative Commons license to my code mainly because it had a nice chooser for helping determine what time of license to use and because it creates a handy little HTML snippet for you to embed in your site.

Layout

I was initially going to add the license to the page footer but this screwed with the layout a bit so I decided to move it to the sidebar along with all the other navigation items.

The license is added as HTML within a text widget, though as it’s only really relevant to the blog posts rather than any of the other site pages I had to install a plugin called Widget Options which allows you to determine on what pages particular widgets are displayed, I set this to only display my copyright widget against posts.

Code

The Creative Commons site provides a HTML snippet for embedding on the desired page, at its most basic this is just an image and a link to the license but it’s also possible to include additional details such as the author and the post title. In order to dynamically populate the copyright widget with page URL, title, author and author URL I needed to edit the functions.php page for my site, this can either be done through the theme editor or directly on the file via an FTP client.

A better option is to use a plugin like My Custom Functions to handle this to make sure the functions don’t get overwritten by theme updates.

add_filter('widget_text', 'do_shortcode');

add_shortcode ('geturl', 'get_current_page_url');
function get_current_page_url() {
	$pageURL = 'http';
	if( isset($_SERVER["HTTPS"]) ) {
		if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	}
	$pageURL .= "://";
	$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	return $pageURL;
}

add_shortcode ('gettitle', 'get_current_page_title');
function get_current_page_title() {
	return get_the_title();
}

add_shortcode ('getauthor', 'get_current_page_author');
function get_current_page_author() {
	return get_the_author();
}

add_shortcode ('getauthorlink', 'get_current_page_author_link');
function get_current_page_author_link() {
	return get_author_posts_url(get_the_author_meta('ID'));
}

The first line enables the use of shortcodes within text widgets, the get_current_page_url function is based on that provided here and the other functions are standard WordPress functions.

These shortcodes are then available to use in text widgets and can be called like so.

<a href="http://creativecommons.org/licenses/by/4.0/" rel="license">
<img style="border-width: 0;" src="https://i.creativecommons.org/l/by/4.0/88x31.png" alt="Creative Commons License" />
</a>
Any code within <a href="https://blog.bitscry.com/2019/02/20/blog-post-code-licensing/">Blog Post Code Licensing</a> by <a href="https://blog.bitscry.com/author/shinigami/">Shinigami</a> is licensed under a <a href="http://creativecommons.org/licenses/by/4.0/" rel="license">Creative Commons Attribution 4.0 International License</a>.

Leave a Reply

Your email address will not be published. Required fields are marked *