Loads of good free WordPress Themes are out there. And the most of them don’t make use (or at least no good use) of the ‘functions.php‘ file. This is how the WordPress Codex describes the file:
A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages). Suggested uses for this file:
* Define functions used in several template files of your theme
* Set up an admin screen, giving users options for colors, styles, and other aspects of your themeThe “Default” WordPress theme contains a functions.php file that defines functions and an admin screen, so you might want to use it as a model. Since functions.php basically functions as a plugin, the Function_Reference list is the best place to go for more information on what you can do with this file.
Ok, there’s a load stuff you can read into about the WordPress functions, but not much is written about this mysterious file or exactly how to use it. Here’s some info:
Open the functions.php file in your theme folder (create the file if it’s not there). Add a function to it between <?php and ?> :
1 2 3 4 | // Function example. Returns the actual date function show_date() { echo date('Y-m-d'); } |
To use the function in your theme, place this code somewhere (i.e. page.php) inside :
1 | <?php show_date(); ?> |
The date should be shown on your page where you inserted the code. Pretty easy, huh. More? Read on…
Including stuff in the template header
For my part I always need to load some stuff in the header, sometimes under certain conditions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (! is_admin() ) { // If we are not in the admin, load stuff add_action('init', 'load_my_script'); // on initialization, execute the function load_my_scripts add_action('wp_head', 'load_my_css'); } function load_my_script() { // load myscript.js version 1.0, which is dependent on 'jquery' and refer to it as 'MyScript' wp_enqueue_script('MyScript', get_template_directory_uri().'/js/myscript.js', array('jquery'), '1.0'); } function load_my_css() { // load myscript.js version 1.0, which is dependent on 'jquery' and refer to it as 'MyScript' echo '<link rel="stylesheet" href="' . bloginfo('template_url') . '/css/style.css" type="text/css" media="screen" />'."\n"; } |
You can check for certain conditions i.e. is_page(), is_home() and so on and add stuff to the header respectively. It makes more sense and saves you bandwidth if you load your JavaScript library which checks the contact form fields for errors only on the page on which the form appears.
Adding custom stuff
Personally I don’t like it when I work on a theme and while testing the theme locally, it needs to load stuff from the internet like the Google analytics code, sponsor banners and other things. I like it quick. The following code is a simple solution. If the page loads local, don’t load external stuff, if it’s on the server in the internet, then load everything. Sometimes, this speeds up things drastically.
In the functions.php add:
1 2 3 4 5 6 7 | // Function to stop external things from being loaded while on localhost function is_local() { $is_devbox = false; $this_host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $this_host == 'localhost' ? $is_devbox = true : $is_devbox = false; return $is_devbox; } |
and in the template add:
1 2 3 4 5 6 7 | <?php if(! isLocal() ) { ?> // Add all your stuff here you don't want to load while on localhost i.e. // your google analytics code. <?php } ?> |
Query the WordPress Database
There’s a widget for the recent posts. What if we don’t want to use widgets in our sidebar? We create the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function get_recent_posts($number) {
// code taken and stripped from wp-includes/widgets.php
if ( !$number || $number <= 5 )
$number = 5;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
else
$number = $number;
$q = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'post_status' => 'publish'));
if ($q->have_posts()) :
?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
<?php endwhile; ?>
</ul>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
endif;
} |
Place the following code in the template where you want to show the links to your latest posts:
1 | <?php get_recent_posts(8); // latest 8 posts ?> |
Or you could include a RSS Feed:
1 2 3 4 5 6 7 8 9 | // Retrieving a RSS feed function getRSS($uri) { // http://codex.wordpress.org/Function_Reference/get_rss $xAbspath = str_replace("\\","/",ABSPATH); // Win/XAMPP compatibility require_once($xAbspath.'/wp-includes/rss-functions.php'); echo '<ul>'; get_rss($uri); echo '</ul>'; } |
And on your page:
1 | <?php getRSS('http://www.flowdrops.com/feed') ?> |
There’s really a lot of functionality you can add to your WordPress theme just by adding php functions to the functions.php file. A good start is the functions.php in the ‘default’ theme folder. It shows also how you can add a theme admin page. Go, modify your theme and add some functionality. But keep in mind: less is often more.











Great post!
Appreciate you.
I was thinking about RSS feed. Now it seems I’ll do that using your code.
December 28th, 2008 8:58 pm