Archive for January, 2012

How to use dig to get the TTL (time to load) of a domain

Obtaining the TTL for a domain is quite simple.

Open up a terminal and meet your new friend ‘dig’.

Type the follow, replacing my website url with your own.

dig +nocmd davidheward.com +noall +answer

The response you will get is:-

davidheward.com.	3496	IN	A	87.124.86.12

Your friend is 3496 this is your domains TTL.

Which is 3,496 seconds, or almost 1 hour.

Enjoy.

Enqueue WordPress script on certain theme template pages only

Enqueuing scripts the proper way in WordPress can help avoid annoying javascript problems and allows us to specify dependancies of our scripts I.E. jQuery, so that WordPress may load the specified scripts before our script, thus eliminating problems.

I quite often find myself wanting to enqueue scripts into one particular page template rather than loosely across my entire site, in a vein attempt to try and keep the site as efficient as I possibly can.

So I thought I’d share with you how to achieve that.

Firstly open up your functions.php file.

if(!function_exists('register_my_scripts')):
    function register_my_scripts(){
    }
endif;

Add the above function. This function will control enqueueing scripts for your theme.
Now we must tell WordPress through registering an action that we want it to check this function when it enqueues scripts in the wp_head(); function called in the header.php file.

We can do this by adding this line of code to our functions.php file.

add_action('wp_enqueue_scripts', 'register_my_scripts');

And wollah, now WordPress will call our function when it attempts to register scripts. Now hopefully you may already be familiar with the WordPress functions wp_register_script and wp_enqueue_script

If not heres a rundown.

wp_register_script
A safe way of registring javascripts in WordPress for later use with wp_enqueue_script().

<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>

Use the wp_enqueue_scripts action to call this function, or admin_enqueue_scripts to call it on the admin side. Calling it outside of an action can lead to problems.

For further details see the wordpress wp_register_script page »

wp_enqueue_script
A safe way of adding javascripts to a WordPress generated page. Basically, include the script if it hasn’t already been included, and load the one that WordPress ships.

usage:

wp_enqueue_script(
     $handle
    ,$src
    ,$deps
    ,$ver
    ,$in_footer
);

Use the wp_enqueue_scripts action to call this function, or admin_enqueue_scripts to call it on the admin side. Calling it outside of an action can lead to problems.

For further details see the wordpress wp_enqueue_script page »

Given that you now have a reasonable grasp of whats going on lets move this forward, first we want to register our script name with WordPress and then we want to enqueue it.

This can be quite easily done with the following.

//Register our script
wp_register_script('my-script',
get_template_directory_uri() . '/assets/js/my-script.js',
array('jquery'),
'1.0' );
//Enqueue the script
wp_enqueue_script('my-script');

Put this inside our earlier defined function of register_my_scripts. First of all I register my-script and then I enqueue it in the load schedule with wp_enqueue_script.

How to do this only on a particular page?

The answer is very easily. Inside the function we can simply wrap the register and enqueue script actions with the following.

if(is_page_template('about.php')):
// code
endif;

Sweet as a nut. See below for the full finished code example.

The full thing

if(!function_exists('register_my_scripts')):
    function register_my_scripts(){
        if(is_page_template('about.php')):
            //Register our script
            wp_register_script('my-script',
            get_template_directory_uri() . '/assets/js/my-script.js',
            array('jquery'),
            '1.0' );
            //Enqueue the script
            wp_enqueue_script('my-script');
	endif;
    }
endif;

Some points

You will notice that in my wp_register_script I have used jquery as a dependancy this means that it will now be loaded by WordPress after jquery, neat huh?

You’ll also notice that I have put a version of 1.0 into the register, this is handy for upgrading the scripts to prevent caching problems, as the version is appended to the end of the file name like so:

http://localhost:8888/my-site/wp-content/themes/my-theme/assets/js/my-script.js?ver=1.0

If you make some changes and want to avoid any caching issues, simply upgrade the version to 1.1 in your code.