2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 22,000 views in 2012. If each view were a film, this blog would power 5 Film Festivals

Click here to see the complete report.

Posted in Uncategorized

“Human readable” date

Here’s a short code snippet you can use if you want your date to be displayed as “Today”, “Yesterday” or “X days ago”. To accomplish this, simply add a filter to the_date.

In my example it shows “X days ago” if it less than 7 days old. Short and sweet!

function mytheme_the_date( $date ) {
$time = strtotime( $date );
$difference = time() - $time;
$days_ago = (int)( $difference / 60 / 60 / 24 );

if ( date( 'Y-m-d' ) === date( 'Y-m-d', $time ) )
   return __( 'Today' );
elseif ( date( 'Y-m-d', strtotime('-1 day') ) === date( 'Y-m-d', $time ) )
   return __( 'Yesterday' );
elseif ( $days_ago < 7 )
   return sprintf( __( '%d days ago' ), $days_ago );

return $date;
add_filter( 'the_date', 'mytheme_the_date' );

Happy coding!

Tagged with: , ,
Posted in Beginner

Using PHP variables in JavaScript with wp_localize_script

There is many ways to pass dynamic variables to JavaScript. An easy way would be just to define the JavaScript variables in the <head> echoing the PHP variables.

This will of course work, but there is a WordPress-way to do it as well.

The function wp_localize_script gives you the ability to write a JavaScript object with multiple variables defined. The function must be called after your script has been enqueued. Simply pass in the name of your script handle, the object name and your data.

function mytheme_wp_enqueue_scripts() {
wp_enqueue_script( 'mytheme_script', get_template_directory_uri() . '/js/main.js', array('jquery'), false, true );
wp_localize_script( 'mytheme_script', 'myObj', array(
'homeUrl' => home_url(),
'translatableString' => __( 'A String' )
) );
add_action( 'wp_enqueue_scripts', 'mytheme_wp_enqueue_scripts' );

This will print the following code before your script is included

<script type='text/javascript'>
/* <!--[<span class="hiddenSpellError" pre=""-->CDATA[ */
var myObj = {"homeUrl":"http:\/\/forsakringskassan.dev","translatableString":"A String"};
/* ]]> */

Then you are free to use the variable in your JavaScript in which ever way you want!


That’s all — Happy coding!

Tagged with: , , , ,
Posted in Intermediate

Use _n to write a phrase in single or plural form

If you would want to write a phrase in single or plural form depending on a number, there is a very handy function for it written in WordPress called _n. The function also takes a $domain argument which can be handy when localizing plugins or themes.

In your search template you might want to print “Found X results matching your query”. When X is above 1 you want to write “results” but if there only was a single result found you would want to write “result”.

This can be accomplished with the following code:

<?php printf( __( 'Found %d %s matching your query: %s' ), $wp_query->found_posts, _n( 'result', 'results', $wp_query->found_posts), get_search_query() ); ?>

More information about _n can be found in the codex!

Tagged with: , , , , ,
Posted in Best Practices, Intermediate

Remove title attribute from WordPress images

I use default WordPress functions to insert images very often. Functions like the_post_thumbnail or wp_get_attachment_image are used in almost every project I do.

By default WordPress adds a title to all of the images printed by these functions, and the title usually is nothing more than the filename.

The result is a lot of title attributes which are pretty much useless and just distracting (while hovering an image for instance).

But thankfully WordPress filters saves us as they often do.

Filter wp_get_attachment_image_attributes and unset the title attribute.

function mytheme_wp_get_attachment_image_attributes( $attr ) {


 return $attr;
add_filter( 'wp_get_attachment_image_attributes', 'mytheme_wp_get_attachment_image_attributes' );

After that you should be set!

Tagged with: , , , , ,
Posted in Intermediate

Different headers and footers using the $name argument

Both the_header and the_footer functions except a $name argument which allows you to specify other headers than the default header.php and footer.php.


<?php get_header( 'special' ); ?>

WordPress would then look for header-special.php in your theme directory. If you want to specify a different footer file it works the same way.

I rarely see it used but it can be quite useful, perhaps if you want a “clean” header with just the <html> and <head>.

Read more in the WordPress Codex:

Tagged with: , ,
Posted in Beginner

Modifying the billing and shipping fields in Jigoshop

I recently discovered Jigoshop. A very nice, lightweight and well coded e-Commerce plugin for WordPress.

After some experimenting I found that the checkout and it’s fields for billing and shipping information where defined in the code and could not be modified in the settings.

It was a problem since they had set State as a required field, which most countries outside of US don’t have. In their forums there were many threads regarding this subject, and the answer the support staff gave was to modify the plugin files (!!) which you never should do.

So here’s an update safe to do it.

Create a plugin or add this to your theme’s functions.php

function jigo_mod_wp() {
 if ( class_exists( 'jigoshop_checkout' ) ) {
 $billing_fields = array(
 array( 'name'=>'billing-first_name', 'label' => __('First Name', 'jigoshop'), 'placeholder' => __('First Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
 array( 'name'=>'billing-last_name', 'label' => __('Last Name', 'jigoshop'), 'placeholder' => __('Last Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
 array( 'name'=>'billing-address', 'label' => __('Address', 'jigoshop'), 'placeholder' => __('Address', 'jigoshop'), 'required' => true ),
 array( 'name'=>'billing-city', 'label' => __('City', 'jigoshop'), 'placeholder' => __('City', 'jigoshop'), 'required' => true ),
 array( 'validate' => 'postcode', 'format' => 'postcode', 'name'=>'billing-postcode', 'label' => __('Postcode', 'jigoshop'), 'placeholder' => __('Postcode', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
 array( 'type'=> 'country', 'name'=>'billing-country', 'label' => __('Country', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
 array( 'name'=>'billing-email', 'validate' => 'email', 'label' => __('Email Address', 'jigoshop'), 'placeholder' => __('***@yourdomain.com', 'jigoshop'), 'required' => true )
 jigoshop_checkout::instance()->billing_fields = $billing_fields;

 $shipping_fields = array(
 array( 'name'=>'shipping-first_name', 'label' => __('First Name', 'jigoshop'), 'placeholder' => __('First Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
 array( 'name'=>'shipping-last_name', 'label' => __('Last Name', 'jigoshop'), 'placeholder' => __('Last Name', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
 array( 'name'=>'shipping-address', 'label' => __('Address', 'jigoshop'), 'placeholder' => __('Address', 'jigoshop'), 'required' => true ),
 array( 'name'=>'shipping-city', 'label' => __('City', 'jigoshop'), 'placeholder' => __('City', 'jigoshop'), 'required' => true ),
 array( 'validate' => 'postcode', 'format' => 'postcode', 'name'=>'billing-postcode', 'label' => __('Postcode', 'jigoshop'), 'placeholder' => __('Postcode', 'jigoshop'), 'required' => true, 'class' => array('form-row-first') ),
 array( 'type'=> 'country', 'name'=>'shipping-country', 'label' => __('Country', 'jigoshop'), 'required' => true, 'class' => array('form-row-last') ),
 array( 'name'=>'shipping-email', 'validate' => 'email', 'label' => __('Email Address', 'jigoshop'), 'placeholder' => __('***@yourdomain.com', 'jigoshop'), 'required' => true )
 jigoshop_checkout::instance()->shipping_fields = $shipping_fields;
} add_action ( 'wp', 'jigo_mod_wp' );

Modify the array and add/remove the fields you want. Some of the items have an array item called required which controls whether a field is required or not.

Always fun to see great plugins that also are pluggable which is a MUST for it to be useful for developers.

Tagged with: ,
Posted in Advanced

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 121 other followers

%d bloggers like this: