ThemeFuse giveaway! 3 premium themes to win

A word about ThemeFuse

ThemeFuse is one of the most popular actors in the premium WordPress themes market. They provide lots of themes with a great design and solid code.

They have lots of different designs for lots of different needs, so you should definitely have a look to their website!

How to enter the contest?

To enter, simply leave a comment on this article to let me know which theme from ThemeFuse WordPress themes you’d like to win. In one week (February 13, 2013), I’ll randomly pick up 3 winners. Each winner will receive the theme of their choice from ThemeFuse.

Good luck everybody!

Modern CSS3 techniques to embellish your website

Black and white images using CSS3

The following CSS class will display any color image in black and white. The vendor prefix allows the trick to work on any browser.

img.desaturate { 
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}

Source/Demo: http://demosthenes.info/blog/532/Convert-Images-To-Black–White-With-CSS

Page top shadow in CSS3

Here is a simple snippet to give your website a nice page top shadow. Easy to apply and visually pleasant!

body:before {
          content: "";
          position: fixed;
          top: -10px;
          left: 0;
          width: 100%;
          height: 10px;

          -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          z-index: 100;
}

Source/Demo: http://playground.genelocklin.com/depth/

Detecting double-clicks in CSS3

Believe it or not, it’s possible to detect when an element has been double-clicked by using just CSS, as demonstrated in the following code:

<div class="test3">
  <span><input type="text" value="&nbsp;" readonly="true" />
  <a href="http://google.com">Double click me</a></span>
</div>

<style type="text/css">
.test3 span {
	position: relative;
}
.test3 span a { 
	position: relative;
	z-index: 2; 
}
.test3 span a:hover, .test3 span a:active { 
	z-index: 4; 
}
.test3 span input { 
	background: transparent; 
	border: 0; 
	cursor: pointer; 
	position: absolute; 
	top: -1px; 
	left: 0; 
	width: 101%;  /* Hacky */
	height: 301%; /* Hacky */
	z-index: 3; 
}
.test3 span input:focus { 
	background: transparent; 
	border: 0; 
	z-index: 1; 
}
</style>

Source/Demo: http://css-tricks.com/examples/CSSDoubleClick/

Triangles in CSS3

Yes, it’s actually possible to draw triangles using only CSS. Although it’s probably not the best way of doing it, I still find this technique pretty useful and interesting.

/* create an arrow that points up */
div.arrow-up {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;  /* left arrow slant */
  border-right:5px solid transparent; /* right arrow slant */
  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points down */
div.arrow-down {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;
  border-right:5px solid transparent;
  border-top:5px solid #2f2f2f;
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points left */
div.arrow-left {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-right:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points right */
div.arrow-right {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-left:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

Source/Demo: http://davidwalsh.name/css-triangles

Using CSS calc()

calc() works like a function and allow you to perform calculations to determine the size and shape of objects. It can be used anywhere a length is required.

/* basic calc */
.simpleBlock {
  width: calc(100% - 100px);
}

/* calc in calc */
.complexBlock {
  width: calc(100% - 50% / 3);
  padding: 5px calc(3% - 2px);
  margin-left: calc(10% + 10px);
}

Source/Demo: http://davidwalsh.name/css-calc

Pure CSS text gradients

Text gradients have always been popular on the internet. Now with CSS3, it’s a lot easier to create beautiful gradients in a matter of minutes.

h2[data-text] {
	position: relative;
}
h2[data-text]::after {
	content: attr(data-text);
	z-index: 10;
	color: #e3e3e3;
	position: absolute;
	top: 0;
	left: 0;
	-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));

Source: http://snipplr.com/view/49911/pure-css-text-gradients/

Disabling pointer events with CSS

The newly introduced pointer-events property allow you to deactivate pointer events on an element. For example, a link with the following class will not be clickable anymore.

.disabled { pointer-events: none; }

Source/Demo: http://davidwalsh.name/pointer-events

Stiched elements in CSS3

The following code snippet shows how to create a nice stitched look around any element. Nice!

p {
	padding: 5px 10px;
	margin: 10px;
	background: #ff0030;
	color: #fff;
	font-size: 21px;
	line-height: 1.3em;
	border: 2px dashed #fff;
	border-radius: 3px;
	-moz-border-radius: 3px;
	-webkit-border-radius: 3px;
	-moz-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
	-webkit-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
	box-shadow: 0 0 0 4px #ff0030, 2px 1px 6px 4px rgba(10,10,0,.5);
	text-shadow: -1px -1px #aa3030;
}

Source: http://www.catswhocode.com/blog/snippets/stitched-elements-in-css3

Custom scrollbars with CSS3 and WebKit

Remember 10 years ago where almost anyone used Microsoft exclusive properties to customize the look of scrollbars? Well, now you can do the same with Webkit.

::-webkit-scrollbar {
  width: 12px;
}

::-webkit-scrollbar-track {
  background: none;
}

::-webkit-scrollbar-thumb {
  background: -webkit-linear-gradient(left, #547c90, #002640);
  border: 1px solid #333;
  box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.4);
}

Source/Demo: http://davidwalsh.name/custom-scrollbars

Blurry text with CSS3

A simple but very nice text blur effect. Easy and good-looking!

.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}

Source/Demo: http://css-tricks.com/snippets/css/blurry-text/

Pure CSS corner ribbon

This code is a bit long, but it creates a fancy corner ribbon in pure CSS.

<div class="wrapper">
       <div class="ribbon-wrapper-green"><div class="ribbon-green">NEWS</div></div>
</div>

And the CSS:

.wrapper {
  margin: 50px auto;
  width: 280px;
  height: 370px;
  background: white;
  border-radius: 10px;
  -webkit-box-shadow: 0px 0px 8px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 8px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 8px rgba(0,0,0,0.3);
  position: relative;
  z-index: 90;
}

.ribbon-wrapper-green {
  width: 85px;
  height: 88px;
  overflow: hidden;
  position: absolute;
  top: -3px;
  right: -3px;
}

.ribbon-green {
  font: bold 15px Sans-Serif;
  color: #333;
  text-align: center;
  text-shadow: rgba(255,255,255,0.5) 0px 1px 0px;
  -webkit-transform: rotate(45deg);
  -moz-transform:    rotate(45deg);
  -ms-transform:     rotate(45deg);
  -o-transform:      rotate(45deg);
  position: relative;
  padding: 7px 0;
  left: -5px;
  top: 15px;
  width: 120px;
  background-color: #BFDC7A;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#BFDC7A), to(#8EBF45)); 
  background-image: -webkit-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:    -moz-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:     -ms-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:      -o-linear-gradient(top, #BFDC7A, #8EBF45); 
  color: #6a6340;
  -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 3px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 3px rgba(0,0,0,0.3);
}

.ribbon-green:before, .ribbon-green:after {
  content: "";
  border-top:   3px solid #6e8900;   
  border-left:  3px solid transparent;
  border-right: 3px solid transparent;
  position:absolute;
  bottom: -3px;
}

.ribbon-green:before {
  left: 0;
}
.ribbon-green:after {
  right: 0;
}

Source/Demo: http://jsfiddle.net/chriscoyier/H6rQ6/1/

WordPress shortcode to easily integrate a Google Map on your blog

To create the shortcode, paste the code below into your functions.php file:

function rockable_googlemap($atts, $content = null) {
   extract(shortcode_atts(array(
               "width" => '940',
               "height" => '300',
               "src" => ''
   ), $atts));
 
return '<div>
         <iframe src="'.$src.'&output=embed" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="'.$width.'" height="'.$height.'"></iframe>
        </div>
       ';
}
 
add_shortcode("googlemap", "rockable_googlemap");

Once you saved your function.php file, you can start integrating Google Maps into your posts and pages. To do so, get the url of the map you’d like to display, and use the shortcode as shown below:

[googlemap src="google_map_url"]

or with custom width and height parameters:

[googlemap width="600" height="250" src="google_map_url"]

Thanks to Rockable Themes for the tip!

Super useful online tools to make JavaScript development easier

JSBeautifier


Is your JavaScript code ugly or hard to read? If yes, no problem: Here is JS Beautifier, a handy tool to make your JS code more prettier and easier to read. JS Beautifier can also deobfuscate code, which can be very useful in several situations.
→ Visit JSBeautifier

JSconsole


When developing JavaScript, debugging is not always easy and finding errors in your code can be very time-consuming. This tool, named, JSconsole, allow easy online debugging of any JS code. A real time-saver!
→ Visit JSconsole

jsFiddle


jsFiddle is a complete playground for web developers. It allows you to edit JavaScript, HTML and CSS snippets and share, embed or tweet them.
→ Visit jsFiddle

JS Mini


Speed is always a concern when it comes to web development. As users don’t like to wait, you have to make sure that your code will be fast to load. To do so, you should definitely make your JavaScript and jQuery code lightweight using this online tool. Code can be reduced up to 60%.
→ Visit JS Mini

JavaScript Obfuscator


Sometimes you might want to prevent from people being able to have a look at your JavaScript code. The best solution is to obfuscate your code and make it a lot harder to understand for people. This handy tool allows you to obfuscate your code online.
→ Visit JavaScript Obfuscator

Regex Pal


Regular expressions are very useful in programming, but they can also be very tricky, especially for beginners. If you need to test a JavaScript regular expression, this website is the perfect tool to do so.
→ Visit Regex Pal

JS Lint


JS Lint is a very useful tool that takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location.
→ Visit JS Lint

JS String Escape


In JavaScript, special characters inside a String can really mess things up. The web server can read the JavaScript file, using the wrong encoding, the users browser can have the wrong encoding setting. A way to avoid that is to use escape codes. This online JavaScript escape tool does that automatically.
→ Visit JS String Escape

JS Pretty


JS Pretty is a free online JavaScript and jQuery beautifier tool that uncompresses and uncompacts your js code so it is readable and aligns it so it looks pretty.
→ Visit JS Pretty

How to display post attachment count in admin post list

Just paste the code below into your function.php file. Once you saved the file, a new column will display the post attachment count on your admin post list.

add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2);
function posts_columns_attachment_count($defaults){
    $defaults['wps_post_attachments'] = __('Att');
    return $defaults;
}
function posts_custom_columns_attachment_count($column_name, $id){
        if($column_name === 'wps_post_attachments'){
        $attachments = get_children(array('post_parent'=>$id));
        $count = count($attachments);
        if($count !=0){echo $count;}
    }
}

Thanks to Kevin Chard for the tip!

Automatically add classes to links generated by next_posts_link and previous_posts_link

Paste the following code in your functions.php file:

add_filter('next_posts_link_attributes', 'posts_link_attributes');
add_filter('previous_posts_link_attributes', 'posts_link_attributes');

function posts_link_attributes() {
    return 'class="myclass"';
}

Thanks to CSS Tricks for the function!

Best of CatsWhoCode (2008 – 2012)

WordPress

PHP

HTML & CSS

JavaScript and jQuery

Web developer tools & resources

Shortcode to display external files on your posts

Open your functions.php file and paste the following function in it:

function show_file_func( $atts ) {
  extract( shortcode_atts( array(
    'file' => ''
  ), $atts ) );
 
  if ($file!='')
    return @file_get_contents($file);
}
 
add_shortcode( 'show_file', 'show_file_func' );

Once done, you can use the shortcode. It’s pretty simple:

[show_file file="http://www.test.com/test.html"]

Thanks to Vladimir Prelovac for the handy tip!

Mighty Deals Christmas giveaway

A word about Mighty Deals

Mightydeals offers unbelievable deals and discounts for creative professionals.The deals include products and services that are heavily discounted, exclusively for Mighty Deals customers, usually from 50% to 90% off. Each deal stays on the site for a very limited time and is available exclusively for purchase directly through the site.

How to enter the contest?

To enter, simply leave a comment to let me know which deal you’d like to win from the available mighty deals. On wednesday 19, I’ll pick 3 winners using random.org. Winners will receive the deal they chose directly from Mighty Deals.

Awesome web tools to simplify front-end development

Form builder


Forms are a very important part of any website, but they can also be quite long and complicated to built. This very handy tool will assist and help you to create efficient web forms. A real time saver!

→ visit form builder

Colllor


Need help with color associations? Here is a useful tool for you. Colllor makes it easier to generate color palettes and variations with just a few clicks.

→ visit colllor

JSconsole


Need to debug your JavaScript code? JSconsole allow easy online debugging of any JS code.

→ visit JSconsole

CleanCSS


CleanCSS is a tool that allow you to make your css stylesheet smaller, cleaner and easier to read. Simply set the settings, paste your valid CSS code, and let CleanCSS do the hard work.

→ visit CleanCSS

Mystic Paste


As you probably know if you often read my blog, I really like to collect and share code snippets. Mystic Paste is a very useful pastebin-like website to paste, share and keep your favorite code snippets.

→ visit Mystic Paste

Dabblet


Do you need to test some HTML? Do you want to see what some CSS code can do? Dabblet is one of my favorite web tools for front-end web development. It allows you to see your CSS and HTML code in action. Very useful for testing purposes.

→ visit Dabblet

Screenqueri.es


In 2012, it is very important that your website is responsive and display correctly in either big screens and portable devices. To test how your website looks in various screen resolutions, I use Screenqueri.es, and you should too!

→ visit Screenqueri.es

Moqups


Moqups is an awesome HTML5 App used to create wireframes online. Super useful when you need to create a quick mockup of a website.

→ visit Moqups

Minus


Images are indeed a very important media on the web. I often need to be able to stock some image, and also to execute basic tasks as such as resizing or cropping. Minus is definitely my favorite website for all those purposes.

→ visit Minus

Font Face Generator


Do you like to use custom fonts on your website? Font Squirrel font-face generator generate all font formats as well as the CSS code needed for using cross-browser compatible custom fonts on your website. A must have in your bookmarks!

→ visit Font Face Generator

WordPress tip: Add a custom field to all posts

Simply run the following SQL query on your WordPress database, using the command line client or PhpMyAdmin. This will automatically ad a custom field named MyCustomField to all your existing posts.
Don’t forget to backup your database before using this query.

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'MyCustomField'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'MyCustomField')
`` AND post_type = 'post';

Thanks to Abhishek for the tip!

WordPress tip: How to get the first link in posts

The first thing to do is to integrate the function to your theme. To do so, paste this code into your functions.php file.

function get_content_link( $content = false, $echo = false ){
    if ( $content === false )
        $content = get_the_content(); 

    $content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links );
    $content = $links[1][0];

    if ( empty($content) ) {
    	$content = false;
    }

    return $content;
}

The function above finds the first link in the post and returns it for you. In this way, you can link the title(or whatever) to this place, as demonstrated below:

<h2><a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a></h2>

Thanks to WP Snippets for the snippet!