Customize your WordPress theme with functions.php magic. WordPress provides you the flexibility of adding functionality to and extending the default capabilities of your current themes by editing functions.php template file; without modifying core WordPress files. Here are 10 WordPress hacks to help you customize your current WordPress theme; just add the following code to your functions.php file and update it to reflect your customizations. (To learn how to edit functions.php please refer to our previous article on How To Edit Functions.php In WordPress)
-
Automatic Feed Links In Header
// add feed links to header
if (function_exists('automatic_feed_links')) {
automatic_feed_links();
} else {
return;
} -
Add Google Analytics In The Footer (Only For Not-Logged-In Visitors)
// add google analytics to footer
function add_google_analytics() {if(is_user_logged_in()) return;
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'); -
Automatic JQuery Inclusion
// automatic 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);
wp_enqueue_script('jquery');
} -
Dynamic Copyright Date In WordPress Footer
//add dynamic copyright date
function dynamic_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}Add the following code to footer.php file:
<?php echo dynamic_copyright(); ?> -
Add Post Thumbnails To RSS feeds
//include post thumbnails in RSS feeds
function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail'); -
Customize Excerpts Length
//modify default excerpt length
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length'); -
Set Minimum Word Count For Posts
//set minimum word count for posts
function minWord ($content)
{
global $post;
$content = $post->post_content;
if (str_word_count($content) < 100 ) //set this to the minimum number of words
wp_die( __('Error: your post is below the minimum word count. It needs to be longer than 100 words.') );
}
add_action('publish_post', 'minWord'); -
Delay Feed Update
// delay rss feed update
function publish_later_on_feed($where) {
global $wpdb;
if (is_feed()) {
// timestamp in WP-format
$now = gmdate('Y-m-d H:i:s');
// value for wait; + device
$wait = '5'; // integer
// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
// add SQL-sytax to default $where
$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
}
return $where;
}
add_filter('posts_where', 'publish_later_on_feed') -
Multiple Widgitized areas
//widgitize header, sidebar and footer areas
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Header',
'id' => 'header',
'description' => 'This is the widgetized header.',
'before_widget' => '<div id="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => 'Sidebar',
'id' => 'sidebar',
'description' => 'This is the widgetized sidebar.',
'before_widget' => '<div id="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>'
));
register_sidebar(array(
'name' => 'Footer',
'id' => 'footer',
'description' => 'This is the widgetized footer.',
'before_widget' => '<div id="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>'
));}
Thereafter, add the following code to the specific files — header.php, sidebar.php and footer.php. The code provided below is for header.php. Replace ‘header’ with ‘sidebar’ and ‘footer’ in respective files.<div id="widgetized-header">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('header')) : else : ?>
<div>
<p><strong>Widgetized Header</strong></p>
<p>This panel is active and ready for you to add some widgets via the WP Admin</p>
</div>
<?php endif; ?>
</div> -
Add Custom Content After Each Post
// add custom post content
function add_post_content($content) {
if(!is_feed() && !is_home()) {
$content .= '<p>This article is copyright © '.date('Y').' '.bloginfo('name').'</p>';
}
return $content;
}
add_filter('the_content', 'add_post_content');
Join 37,807 others and get free tutorials & tips on design & development using Wordpress on, Thesis & Genesis!
Need help with Wordpress, Thesis or Genesis?
Are you a designer or a developer looking for a hand with Wordpress, Thesis or Genesis? Or perhaps want to save time with a project? Get in touch and let's get the ball rolling! Check out our Thesis development, PSD to Thesis & other services.
