A collection of useful WordPress functions
Posted: by Steffen JørgensenThe functions.php file in every WordPress theme can contain many useful functions — everything from adding post thumbnail support to custom posttypes and your own taxonomies.
In this post I have collected a lot of functions that can help you customize your WordPress installation. Some are very useful, others are just nice workarounds of common challenges — some you might not want to use.
Remember — I’m not saying you should use ALL of the functions, or just copy/paste the whole code to your functions.php file. Only use the snippets you need as some of the code can do really bad things, depending on your setup.
Originally I came across a post over at digwp.com. They had 2 articles dedicated to functions.php, and I desided to work from there; customize to fit my needs and adding a few of my own functions.
// Post thumbnails support
add_theme_support('post-thumbnails');
// Menu support
add_theme_support('menus');
// Post editor styling
add_theme_support('editor-style');
// Post formats support
add_theme_support( 'post-formats', array( 'aside', 'gallery','link','image','quote','status','video','audio','chat' ) );
// Clean up header elements
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
// remove adminbar
remove_action( 'init', 'wp_admin_bar_init' );
add_action( 'show_admin_bar', '__return_false' );
// Permalinks with ÆØÅæøå (and other special characters)
if ( !function_exists('sanitize_title_dk') )
{
$translate_language = 'da';
$translate_system = array();
function sanitize_title_dk($title)
{
global $translate_language, $translate_system;
if (!mb_check_encoding($title, 'ASCII'))
{
//$title = utf8_decode($title);
$translate_system[] = array ( "æ" => "ae", "ø" => "oe", "å" => "aa", "Æ" => "ae", "Ø" => "oe", "Å" => "aa" );
foreach ($translate_system as $system)
{
$title = strtr($title, $system);
};
};
return $title;
};
add_filter('sanitize_title', 'sanitize_title_dk', 0);
};
// add feed links to header
if (function_exists('automatic_feed_links')) {
automatic_feed_links();
} else {
return;
}
// smart jquery inclusion
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '1.7.1');
wp_enqueue_script('jquery');
}
// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
add_action('get_header', 'enable_threaded_comments');
// add google analytics to footer
function add_google_analytics() {
echo '<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>';
echo '<script type="text/javascript">';
echo 'var pageTracker = _gat._getTracker("UA-XXXXX-X");';
echo 'pageTracker._trackPageview();';
echo '</script>';
}
add_action('wp_footer', 'add_google_analytics');
// custom excerpt length
function custom_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');
// no more jumping for read more link
function no_more_jumping($post) {
return '<a href="'.get_permalink($post->ID).'" class="read-more">'.'Continue Reading'.'</a>';
}
add_filter('excerpt_more', 'no_more_jumping');
// add a favicon to your
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');
// add a favicon for your admin
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/images/favicon.png" />';
}
add_action('admin_head', 'admin_favicon');
// custom admin login logo
function custom_login_logo() {
echo '<style type="text/css">
h1 a { background-image: url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');
// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
//if (is_home())
$sidebars_widgets = array(false);
return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');
// kill the admin nag
if (!current_user_can('edit_users')) {
add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
add_filter('pre_option_update_core', create_function('$a', "return null;"));
}
// category id in body and post class
function category_id_class($classes) {
global $post;
foreach((get_the_category($post-<ID)) as $category)
$classes [] = 'cat-' . $category-<cat_ID . '-id';
return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');
// get the first category id
function get_first_category_ID() {
$category = get_the_category();
return $category[0]-<cat_ID;
}
// Custom user profile fields
function my_custom_userfields( $contactmethods ) {
// Add contact custom fields
$contactmethods['contact_phone_office'] = 'Office Phone';
$contactmethods['contact_phone_mobile'] = 'Mobile Phone';
$contactmethods['contact_office_fax'] = 'Office Fax';
// Add address custom fields
$contactmethods['address_line_1'] = 'Address Line 1';
$contactmethods['address_line_2'] = 'Address Line 2 (optional)';
$contactmethods['address_city'] = 'City';
$contactmethods['address_state'] = 'State';
$contactmethods['address_zipcode'] = 'Zipcode';
return $contactmethods;
}
add_filter('user_contactmethods','my_custom_userfields',10,1);
// Enable gzip
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));