Loading WordPress Scripts/Styles Correctly

As a developer there are lots of times when developing WordPress themes when you want to add links to javascript and/or stylesheets in your themes to give extra functionality etc. The usual method of adding calls to these into the head of your header.php file is fine, but there is a better way, using a built in WordPress function. In this post I will outline the best ways to add these to your themes or plugins.

Often when building a theme you will want to add code to the head of your page such as javascript and stylesheet files. WordPress contains a couple of pretty cool functions that give you some additional functionality that just adding the code the head doesn’t give. The functions that you need are outlined below:


As you can see there are two functions associated with scripts and two with styles. The wp_register_script() allows you to tell WordPress which scripts you want to use and the wp_enqueue_script() tells WordPress to add them to the head tag inside the wp_head() function. The same can be said for wp_register_style() and wp_enqueue_style().

Lets look at an example of you may want to use these in order to add a load a javascript file into your theme. In this example we will imagine that your javascript file is called slider.js and is located in your themes folder in a sub folder called scripts. The first thing we need to do is to register the script file which tells WordPress where the file is located. Here is the code for this:

<?php wp_register_style( 'my_slider', bloginfo('template_url') . '/scripts/slider.js' ); ?>

As you can see above the first parameter in the wp_regsiter_script() function gives our javascript file a name. It is really important that this is a unique name and therefore I would highly recommend that like all functions you create you prefix your functions and names in functions.php. Here I have just prefixed it with the word ‘my’.

Now WordPress knows the location of the javascript file we need to use the wp_enqueue_script() function to actually add this to the head of the page. Here is the code for this:

<?php wp_enqueue_script( 'my_slider' ); ?>

As you can see here we pass the name that we gave to our javascript file when we registered it to the wp_enqueue_script() so that WordPress adds it to the wp_head() part of your theme. That should do it and the style functions mentioned work in the same way. Register your style and give it a unique name and then add it with wp_enqueue_style(). The only other thing that you might want to do is to create a function for this. To do this the entire code would then become:

function my_scripts_styles() {
wp_register_style( 'my_slider', bloginfo('template_url') . '/scripts/slider.js' );
wp_enqueue_script( 'my_slider' );
add_action('init', 'my_scripts_styles');

So I suppose you might be wondering why going through all that is better than adding them to the head of your header file in your theme yourself. Well the main reason is that using these functions, WordPress checks to see if any other plugin etc has already loaded the files that you are asking for and then if it has it does not load them twice. If they were added manually then there is a chance that javascript files could be loaded twice which could lead to errors.

I was inspired to write this post after watching Chelsea Otakan presentation at WordCamp Pheonix on Typography and WordPress.

Using P2 Theme for a Development Log

Over at Pixel Junction Creative we have a number of developers and designers working on projects together and we were getting increasingly frustrated with having to email each other all the time to keep everyone up-to-date and in the loop with what was going on. To get around this I came across the P2 WordPress theme developed by Automattic. This has provided a great solution to our needs and I wanted to talk about how we have implemented the theme in order to create a development log.

P2 is a theme for WordPress that transforms a mild-mannered blog into a super-blog with features like inline comments on the homepage, a posting form right on the homepage, inline editing of posts and comments, real-time updates so new posts and comments come in without reloading, and much more.

Having downloaded and installed the theme it was clear that it was almost as we required it, but as always these things always need a few custom bells and whistles to make them just as you want them. In this case most of the changes were in terms of design, but functionality could change too.

[wpvideo YYNW9iSj w=610 h=343]

To go about making the changes I decided that the best way forward would be to create a child theme, based on the main P2 theme. This child theme would then be used for the development log site. The child theme being used simply contains a stylesheet (style.css) and functions file (functions.php) and a screenshot (screenshot.png).

The stylesheet is where the design changes come into play although the P2 Theme has many options in the backend in order to change various elements such as the header and the colours etc. The functions file was necessary to add some functionality that was not present in the theme itself. The main change in functionality is that I wanted to make the entire site accessible to logged in, registered users only. To do this I used my own Logged In Users Only Plugin. This code went straight into the functions file rather than having to activate a plugin, that way once the theme was active the site would always be protected to logged in users only.

The other code that was added to the functions file was simply some code to remove some of the menus from the dashboard to make it more foolproof for all the non WordPress designers and developers

The benefits of the P2 theme are great. We can track the development of projects by tagging the project with each update as well as keeping a track of all documents for a particular project as these can be uploaded from the front end just the same as posting. It also enables us to see a history of the development of the project as all the discussion can be seen by clicking on the specific tag for a project.

Once addition that we have made is to add to the log the Email Alerts plugin. This plugin sends email alerts to all registered users that have selected this option on their profile page for both comments and posts. This enables all the developers and designers to keep track of when the log has been updates and when they need t check it etc.

This is a really good solution to the problem of collaboration between a number of people and works very well. With a few minor alteration here and there we now have a fully function development log for keeping track of the different projects that everyone is working on.

Why I’m Not Keen on WordPress Theme Frameworks

WordPress theme frameworks are a relatively new thing. In fact they seem to kick off when WordPress introduced the use of child themes back when WordPress 2.7. was released. However they have really caught on over the last couple of years, with many different frameworks now available for use. Here I want to talk about how theme frameworks are built to be used and my thoughts on their success.

As mentioned most theme frameworks rely on the parent and child themes process. You create a child theme based on the parents theme framework and therefore when the framework is update you changes are not lost. Most of the frameworks that I have seen out their including one I have used extensively, Thematic are essentially a parent theme. That is they are a WordPress theme that you add to your wp-content/themes/ folder. You then build a theme using this parent theme framework as your parent theme. This means you have access to all the frameworks functions and code.

So that all sounds great, but why therefore am I not that keen on using theme frameworks. Well I think it comes down to the fact that I believe that theme frameworks at the moment are going about this the wrong way. They way they are at the moment is more restrictive when they are supposed to add functionality rather than preventing theme authors from doing things in certain ways.

I created a number of child themes for a client recently using the Thematic theme framework. The idea of these child themes was that the parent theme (the framework) could be updated and this would not affect the child themes display at all. However in order to get the design and functionality elements that I wanted I ended up with child theme functions.php files that we a couple of 100kb each and about 5 or 6 theme template files that would overwrite the template files in the parent framework theme. Surely this cannot be right as child themes are supposed to be lightweight and really on contain a styles.css file and a functions.php file so that all the bulk is used from the parent theme.

To me a better way of doing things would be to make it so that the framework was not a theme at all. In fact is was simple a folder that could be added to your own parent theme and then activated with a line of code (like a PHP include) in your themes functions.php file. This seems to be the approach that themify.me have taken with their theme framework which works well.

I realise that this may well cause a great deal of debate. Maybe I am doing something wrong with the use of parent theme based frameworks. I would love to hear peoples thoughts on this.

Backup, Backup & Backup Again!

If you are like me then you have probably been using computers for many years and have accrued thousands of files on your computers hard disc, from things like photos and videos to documents and spreadsheets. Have you ever thought about backing these files up to keep them secure in case your computer fails? If not then this post is going to outline some of the options.

If you have never had a computer fail on your or a hard disc fail then you are a fortunate person, like me. They are rare events although they do seem to be having more and more. But there are many other reasons for you to make sure that you have a backup of all the files on your computer. For example if the worst should have and there is a fire in your house or a car crash, breaks your laptop, you need to have a backup copy of your work. I have at least 50gb of photos and videos on my computer that are irreplaceable should the worse happen. If they were lost those memories would never be able to be retrieved. For this reason along you need to backup.

Backup Options

So what are your options for backing up? Well as usual in my experience things get a whole lot easier here if you have a Mac rather than a PC running Windows. Macs come built in with backup software called Time Machine. Time Machine IconThis software works by backing up the entire contents of your computers hard disc (everything) to an external drive (either USB or over a network) every hour. It then keeps these backups and they grow over time. The software works without any input from the user (except a touch on initial setup to tell it which disc to use) and then it does all the work in the background. Seriously if you are not using Time Machine and you have a Mac, you need to start now and set it up to start backing up your computer.

With Windows there are backup software solutions out there. However in my experience they can often be expensive and moreover complicated solutions to a simple problem and then just never seem to be that good in my opinion. Therefore I have always resorted to the manual way on a Windows machine. The first step is to make sure that all of your files (photos, videos, documents, basically everything except your programs or applications) are stored in your User folder (what was My Documents and is now called Documents on Windows 7). I then entered a reminder into my calendar (either on your phone or Outlook etc) to remind me to backup every Sunday (or whatever day suits you). Then at that time I would simply take a copy of that folder to an external drive so that you have a weekly copy of all your files.

Remote Backup

I mentioned above about if the worst should happen. Most of us that do backup store our backup drives either in the same house as the machine you are backing up, or those with a laptop perhaps store the portable hard drive in the same laptop bag as the laptop drive. Therefore an accident should occur that involves the computer then it would mean that the backup drive suffers the same fate.

The solution to this of course is to have some sort of remote backup solution. The most basic form of this would be to take a backup and then take the drive and store it elsewhere. For example you could take it to work and store it there or to a friend or neighbours. You must however make sure that you continue to backup to this disc at regular intervals so that you have the latest up to date files.

The other alternative is a remote backup solution over the internet, where you files are stored ‘in the cloud’ on the net and therefore in a different locations. Many firms offer this solution. This is not something that I tried, although I have used Dropbox which is a very good service. It would be interesting to hear what everyone uses in terms of online backup.

My perfect solution would be a online service which you could use with Time Machine. Therefore when setting up Time Machine you could select your ‘cloud’ disc and backups are made to a remote location rather than on an external drive in the same location as the computer.

So if you take one thing from having read this then let it be to make you go and backup your files now!