I recently read an article over at Digging Into WordPress which talks about default functions to include in your WordPress functions.php file. Inspired, I figured I’d share my default functions also.
Whenever I begin a new WordPress project, I always include my default functions.php file template. This saves on development time as it has a standard set of functions that I utilize throughout most of my and my clients’ projects. Some are short and simple while others are pretty complex and involved. We’ll concentrate on the short ones in this article and address the longer ones in a future article.
Automatically Add Feed Links to the <head> Section
Since version 2.8, WordPress has had the capability of generating all relevant RSS feeds for your site. However, in order to enable this functionality, we have to add the following to our functions.php file:
// add feed links in <head>
if ( function_exists('automatic_feed_links') ) {
automatic_feed_links();
};
This checks to see if we’re using a compatible version of WordPress (2.8 or newer), and if so, generates the relevant feed links in the <head> section of your site.
Note: This assumes you don’t currently have feed links in your head section. This method will add duplicate feed links if you do. Be sure to check your header.php file for any manually‐added links before using this function.
Note: As of the writing of this article, the current WP version is 2.9.2, the automatic_feed_links() is deprecated as of version 3.0, so be sure to check out this Codex article on new functionality (add_theme_support('automatic-feed-links')) coming in WP 3.0.
Add a Favicon Link
Promote your site in your users’ bookmarks, add a favicon to your site. One way you could do this is to upload a favicon.ico image file into your root directory so that browsers can detect it when someone bookmarks your site. Another way is to include a direct link to the image file in your <head> section like so:
// add a favicon link
function favicon() {
echo "\n".'<link rel="shortcut icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/img/icon/favicon.ico">';
};
add_action('wp_head', 'favicon');
Where get_bloginfo('template_directory') is the path to the template directory and /img/icon/favicon.ico is the path to the favicon within the template directory. As an example, this script echos out the url to the favicon image for this site like so:
<link rel="shortcut icon" type="image/x-icon" href="http://darrinb.com/wp-content/themes/dbdb/img/icon/favicon.ico">
Depending on how you organize your images for your theme, just change the path for the favicon accordingly.
Get the Last Page of a Paginated Post
Often times you only want certain content showing up on the last page of a paginated post. This quick function checks to see if the viewer is on the last page and returns true if they are.
/**
* Check to see if user is on the last Post page
* updated: 4/15/10
*/
function dbdb_is_last_page() {
global $page, $numpages, $multipage;
if ( $multipage ) {
return ( $page === $numpages ) ? true : false ;
} else {
return true;
};
};
Basically it checks to see if the post if a multi‐page post, and if so, which page the viewer is on. If the post isn’t multi‐page, it returns true since the single page is the last page. Check this article out for a more detailed look at this function.
Get the First Page of a Paginated Post
While I find myself frequently using the Last Page function, I recently found a need for the following First Page function:
/**
* check to see if user is on the first Post page
* updated: 4/15/10
*/
function dbdb_is_first_page() {
global $page, $multipage;
if ( $multipage ) {
return ( $page == 1 ) ? true : false ;
} else {
return true;
};
};
For example, I use this function on this site to display the post excerpt only on the first page of a post. Like so:
<?php if( dbdb_is_first_page() ) { ?>
<div class="post-excerpt"> <!-- post-excerpt -->
<?php
if ( '' != $post->post_excerpt ) { ?>
<?php echo $post->post_excerpt;?>
<?php } else { ?>
<p><?php mish_wp_snapshot(get_the_content(), 50, '… <em>(Continues below.)</em>');?></p>
<?php }; ?>
</div> <!-- /post-excerpt -->
<?php }; ?>
What this script does is check if we’re on the first page of the post, if so, then it checks to see if there is a manual post excerpt created, if so, show it, if not, use the WP Snapshot (← shameless plug) plugin to create an automatic excerpt.
Remove Some of Default Stuff WordPress Outputs in the <head> Section
As many WordPress devs know, WP spits out a ton of stuff in the <head> section of sites. Just view the source of one your pages to see what I mean. This little snippet below (with comments) will remove some of that extra cra…, err, code and really clean things up.
/** * Remove some default WP settings * updated: 4/15/10 */ remove_action( 'wp_head', 'index_rel_link' ); // remove index link remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // remove prev link remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // remove start link remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // remove relational links remove_action( 'wp_head', 'wlwmanifest_link' ); // remove Windows Live Writer manifest file remove_action( 'wp_head', 'feed_links_extra', 3 ); // remove extra feeds (category feeds) remove_action( 'wp_head', 'rsd_link' ); // remove link to Really Simple Discovery service endpoint (EditURI)
Remove WordPress Version Number
A common form of security used in WP sites is the removal of the version number from the meta generator tags WP outputs. The idea being a sort of “security by obscurity” — harder to hack if they don’t know what version of WordPress you’re using. The issue I take with most of the solutions I’ve found is that they completely strip the generator tag altogether. Since I’m all about promoting WordPress, but still want to maintain as secure a site as possible, I came up with this handy script that strips the version number from the generator tags, but still outputs the WordPress brand:
// remove version info from head and feeds, but still plug WordPress
add_filter('get_the_generator_html', create_function('', 'return "<meta name=\"generator\" content=\"WordPress\">";'));
add_filter('get_the_generator_xhtml', create_function('', 'return "<meta name=\"generator\" content=\"WordPress\" />";'));
add_filter('get_the_generator_atom', create_function('', 'return "<generator uri=\"http://wordpress.org/\">WordPress</generator>";'));
add_filter('get_the_generator_rss2', create_function('', 'return "<generator>http://wordpress.org/</generator>";'));
add_filter('get_the_generator_rdf', create_function('', 'return "<admin:generatorAgent rdf:resource=\"http://wordpress.org/\" />";'));
add_filter('get_the_generator_comment', create_function('', 'return "<!-- generator=\"WordPress\" -->";'));
add_filter('get_the_generator_export', create_function('', 'return "<!-- generator=\"wordpress\" created=\"'. date('Y-m-d H:i') . '\"-->";'));
Conclusion
The scripts listed here are the more basic functions I include in my functions.php. In another article we’ll discuss some of the more advanced functions that are time savers and additional security features