{"id":149807,"date":"2015-12-08T11:00:00","date_gmt":"2015-12-08T16:00:00","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=149807"},"modified":"2015-12-08T06:26:37","modified_gmt":"2015-12-08T11:26:37","slug":"christmas-code-snippets","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/christmas-code-snippets\/","title":{"rendered":"4 Fun Code Snippets to Add Christmas Cheer to Your WordPress Site"},"content":{"rendered":"<h2>With Christmas almost upon us, I&#8217;ve coded up some fun holiday snippets\u00a0to get your website ready for the holidays.<\/h2>\n<p>Below you&#8217;ll find snippets for a full CSS Santa, falling snow, adding a festive scene to your site, and a subtle Christmas tree background.<\/p>\n<p>If you would prefer to use a plugin, check out our \u00a0want to check out <a href=\"https:\/\/wpmudev.com\/blog\/free-wordpress-christmas-plugins\/\" target=\"_blank\">10+ Free WordPress Plugins to Add Christmas Cheer to Your Site<\/a>.<\/p>\n<p>Enjoy and happy holidays!<\/p>\n<p><!--more--><\/p>\n<h3>Getting Started<\/h3>\n<p>For this post, I will be adding all of my code to a child theme, which is WordPress best practice when making modifications to theme files. If you&#8217;re not sure how to do this or need a reminder, our post\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\">How to Create a WordPress Child Theme<\/a> contains all you need to know.<\/p>\n<p>All examples in this posts have been tested in the Twenty Sixteen theme, the new default theme for 2016.<\/p>\n<p>To get going with a child theme, create a new folder in your site&#8217;s &#8220;themes&#8221; directory and name it &#8220;twentysixteen-christmas-edition.&#8221; Create a file named &#8220;style.css&#8221; and &#8220;functions.php&#8221; in there and add the following code in each of these files, respectively:<\/p>\n<div class=\"gist\" data-gist=\"cc55f4cddfb1fd50b0cf41c9349d4063\" data-gist-file=\"basicstyle.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/cc55f4cddfb1fd50b0cf41c9349d4063.js?file=basicstyle.css\">Loading gist cc55f4cddfb1fd50b0cf41c9349d4063<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<div class=\"gist\" data-gist=\"bae68aac30843e23d88c7f31f9e8ca29\" data-gist-file=\"basicfunctions.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/bae68aac30843e23d88c7f31f9e8ca29.js?file=basicfunctions.php\">Loading gist bae68aac30843e23d88c7f31f9e8ca29<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>You should now be able to activate the child theme and get to work on the examples below.<\/p>\n<h3>1. Full CSS Santa<\/h3>\n<p>For my first trick, I&#8217;ll rely heavily on the\u00a0<a href=\"http:\/\/codepen.io\/alikhalilifar\/pen\/bxkzu\" target=\"_blank\">Pure CSS Santa<\/a> Codepen from Ali Khalilifar, which creates a Santa using CSS.<\/p>\n<p>I want to automatically add this icon to any post that is published on December 24 or 25. Ali has done most of the work for us, so all you need to do is copy and paste.<\/p>\n<p>Here&#8217;s an example of what the icon will look like on the front-end of my site:<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/santa-css.png\" alt=\"Santa CSS\" width=\"714\" height=\"735\" \/><figcaption class=\"wp-caption-text\">Our Santa Icon within the post<\/figcaption><\/figure>\n<\/div>\n<p>We&#8217;ll need to attack this problem in a child theme because there&#8217;s no good hook to use in the side section of the post lists. Upon some exploration, I found that the function responsible for outputting that aside is named <code>twentysixteen_entry_meta()<\/code>, and it can be found in the <code>inc\/template-tags.php<\/code> file in the parent theme. The function is wrapped in a\u00a0<code>function-exists()<\/code> check, which means we can overwrite it in our child theme. I started by copy-pasting the whole thing into my child theme&#8217;s functions.php file. I then added the following code to the end:<\/p>\n<div class=\"gist\" data-gist=\"267679e6c3f493fc5122c797396b26ed\" data-gist-file=\"santa-end.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/267679e6c3f493fc5122c797396b26ed.js?file=santa-end.php\">Loading gist 267679e6c3f493fc5122c797396b26ed<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This makes sure that the <code>ch2015_santa_icon()<\/code> function is called only if the month is December and the day is the 24th or 25th. I&#8217;ve also excluded our Santa from the search page because the meta section is different there. If you&#8217;d like, you can include a smaller variant there.<\/p>\n<p>The <code>ch2015_santa_icon()<\/code> function simply outputs the HTML structure for Codepen, like so:<\/p>\n<div class=\"gist\" data-gist=\"131480073674bef7f405f8229302e799\" data-gist-file=\"santa-icon.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/131480073674bef7f405f8229302e799.js?file=santa-icon.php\">Loading gist 131480073674bef7f405f8229302e799<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The final piece of the puzzle is to add the CSS needed to breathe life into the HTML structure and make it Santa. Copy-paste all but the first two rule blocks into your child theme&#8217;s CSS file:<\/p>\n<div class=\"gist\" data-gist=\"1fc68b819b7223d5faa9aff5be4bf1d3\" data-gist-file=\"santa.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/1fc68b819b7223d5faa9aff5be4bf1d3.js?file=santa.css\">Loading gist 1fc68b819b7223d5faa9aff5be4bf1d3<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I&#8217;ve modified the initial ruleset (applied to <code>.santa<\/code>) to get the positioning right. This meant removing margins and making sure the icon was aligned to the left.<\/p>\n<h3>2. Falling Snow<\/h3>\n<p>Twenty Sixteen&#8217;s thick bordered design gives us the opportunity to add some snow effects without overwhelming the screen, which means snow just within the border. Since the border is actually the body background showing through, our job will be pretty easy.<\/p>\n<p>We&#8217;ll make three backgrounds of different sizes and animate them. This will give a parallax-like effect \u2013 the largest image will seem to move fastest because of its size. The three sizes will be 500&#215;500, 400&#215;400 and 300&#215;300. The 500px wide option will need to travel 500 pixels in the same time as the 300px one needs to travel 300pixels, hence the speed difference.<\/p>\n<p>You can even make the snow flake images yourselves! Create a 500&#215;500 image and add a couple of snowflakes to it. I used simple white circles on a black background. I used Photoshop&#8217;s offset filter to make sure the image was tileable. This is required to make sure the animation loops smoothly. This filter is in Photoshop under <strong>Filters &gt; Other<\/strong> and you should set it to half the size of your image. When you&#8217;re done, hide the background black and save the image as a transparent GIF within an &#8220;images&#8221; folder in your child theme. Repeat the process to make a 400 and 300 pixel version. Feel free to play around with the opacity and blur along the way.<\/p>\n<p>If you&#8217;d like to use mine you can grab <a rel=\"lightbox[149807]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/snow-1.png\" target=\"_blank\">snow-1.png<\/a>, <a rel=\"lightbox[149807]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/snow-2.png\" target=\"_blank\">snow-2.png<\/a> and <a rel=\"lightbox[149807]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/snow-3.png\" target=\"_blank\">snow-3.png<\/a> here. You might want to right-click the link and select &#8220;save as,&#8221; as it will be a lot easier to save.<\/p>\n<p>Now we need to add the images as a background to the body element and add the CSS animations:.<\/p>\n<div class=\"gist\" data-gist=\"56c0e0268b0f97a94e00a4646a9960ba\" data-gist-file=\"snowfall.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/56c0e0268b0f97a94e00a4646a9960ba.js?file=snowfall.css\">Loading gist 56c0e0268b0f97a94e00a4646a9960ba<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>In the body element we reference our &#8220;snow&#8221; animations, telling the browser to animate linearly, lasting 20 seconds, looping infinitely.<\/p>\n<p>The keyframes make the images move from to the specified pixel positions and since they&#8217;re all transparent it will seem like the snowflakes are moving.<\/p>\n<figure id=\"attachment_149859\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-149859\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/snowfall.gif\" alt=\"Snowfall\" width=\"735\" height=\"700\" \/><figcaption class=\"wp-caption-text\">Our snowfall animation around the border<\/figcaption><\/figure>\n<h3>3. A Christmas Scene<\/h3>\n<p>Let&#8217;s continue by adding a Christmas scene and greeting in the website footer. I&#8217;ll be using Texx Smith&#8217;s cutesy <a href=\"http:\/\/codepen.io\/texxs\/pen\/WbxYzx\" target=\"_blank\">Christmas Animation 2014<\/a> as the basis for this snippet, but this time we&#8217;ll be modifying the code a bit.<\/p>\n<p>Let&#8217;s start by copying <code>footer.php<\/code> from the parent theme to the child theme of your site.<\/p>\n<p>Once done, open up the copy of the file in yourchild theme and find the end of the site footer. It should look like this: <code>&lt;\/footer&gt;&lt;!-- .site-footer --&gt;<\/code> Right after, paste the following HTML structure:<\/p>\n<div class=\"gist\" data-gist=\"f35f0ef1d09691b47b79ae73c184f280\" data-gist-file=\"scene.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f35f0ef1d09691b47b79ae73c184f280.js?file=scene.php\">Loading gist f35f0ef1d09691b47b79ae73c184f280<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I&#8217;ve removed the elf character and the snowfall elements from Texx&#8217;s code and renamed two classes, otherwise it&#8217;s much the same.<\/p>\n<p>The next step is to add the required CSS to make the scene come alive:<\/p>\n<div class=\"gist\" data-gist=\"91b9d9e4db3173fcceed8e87cf47d0a7\" data-gist-file=\"scene.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/91b9d9e4db3173fcceed8e87cf47d0a7.js?file=scene.css\">Loading gist 91b9d9e4db3173fcceed8e87cf47d0a7<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This is fairly standard stuff, note the animations for the window light at the end followed by the styling of the text at the bottom.<\/p>\n<p>Once you&#8217;re done you should get a nice Christmas scene in the footer of your site.<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/christmas-scene.jpg\" alt=\"Christmas Scene\" width=\"735\" height=\"548\" \/><figcaption class=\"wp-caption-text\">Christmas Scene<\/figcaption><\/figure>\n<\/div>\n<h3>4. Subtle Christmas Tree Background<\/h3>\n<p>This is a simple one, a subtle background image. I found a great little pattern over at <a href=\"http:\/\/subtlepatterns.com\/new-year\/\" target=\"_blank\">Subtle Patterns<\/a>. Download it now and add the PNG file to the &#8220;images&#8221; folder in your child theme.<\/p>\n<p>Despite it&#8217;s subtlety, it is still a bit strong to put behind text so we&#8217;ll bump down the opacity. Due to this, we can&#8217;t simply add it to the <code>#page<\/code> element of your site(the large white container). We&#8217;ll attach it to the <code>:before<\/code> pseudo-selector and make it stretch all the way.<\/p>\n<p>Here&#8217;s the CSS that will make it happen:<\/p>\n<div class=\"gist\" data-gist=\"a74e9455390dee3d1aa8e3b7c7cee2c3\" data-gist-file=\"background.css\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/a74e9455390dee3d1aa8e3b7c7cee2c3.js?file=background.css\">Loading gist a74e9455390dee3d1aa8e3b7c7cee2c3<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Play around with the opacity value to get the strength of the pattern just right.<\/p>\n<figure id=\"attachment_149865\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-149865\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/12\/tree-background.png\" alt=\"Subtle Tree Background\" width=\"735\" height=\"591\" \/><figcaption class=\"wp-caption-text\">Subtle Tree Background<\/figcaption><\/figure>\n<p>And there you have it, four great festive\u00a0code snippets to get your site ready for the holiday\u00a0season.<\/p>\n<p><strong>If you add any of the snippets above to your site, leave a link in the comments below. I&#8217;d love to see how you use them on your site!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With Christmas almost upon us, we&#8217;ve put together some festive snippets to add holiday cheer to your site, from a CSS Santa to falling snow and a stunning Christmas tree background. Enjoy!<\/p>\n","protected":false},"author":344049,"featured_media":149845,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[10030],"tutorials_categories":[],"class_list":["post-149807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-christmas"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=149807"}],"version-history":[{"count":15,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149807\/revisions"}],"predecessor-version":[{"id":196541,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/149807\/revisions\/196541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/149845"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=149807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=149807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=149807"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=149807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}