Playing with the theme functions file in Wordpress

I’ve recently discovered the power of the Themes Function page in your Wordpress theme. It lets you achieve the same thing as if you’d hacked a core file (which would get overridden with every update – pain in the neck). For example, I had a site where I wanted to use The Excerpt Reloaded and Thumbnail for Excerpts on the front page. Unfortunately, they wouldn’t work together. All I really needed from The Excerpt Reloaded was a nice “Read more” link instead of that stupid ellipsis Wordpress insists on putting. A little more research showed me that all I needed to do was insert some code into my Theme Functions file.

Since I’m not a php coder, I have to rely on others to write functions for me. If you can write your own functions, you’ll be ahead of the game. I got this code from DNExpert:

remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);
function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed

if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text, ‘<p>’);
$excerpt_length = 50;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘ … <a href=”‘. get_permalink() . ‘”>READ MORE</a>’);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

There are two settings you can customize in there: $excerpt_length, and the exact words you want for your permalink (see array_push, and alter the phrase “READ MORE”).

I also found another neat trick somewhere in the Wordpress help files. I like to have as few lines of code between the top of my page and my first post as possible, and more recent versions of Wordpress inserts a couple of unecessary ones:

<link rel=”EditURI” type=”application/rsd+xml” title=”RSD” href=”http://ChillyCool.com/xmlrpc.php?rsd” />
<link rel=”wlwmanifest” type=”application/wlwmanifest+xml” href=”http://ChillyCool.com/wp-includes/wlwmanifest.xml” />

And there’s no obvious way to get rid of them, except by putting the following code into your Themes Function file:

remove_action(‘wp_head’, ‘rsd_link’);
remove_action(‘wp_head’, ‘wlwmanifest_link’);

So how do you insert this code, exactly? Your existing Themes Function file will look something like this (it can vary, depending what your theme designer has done with it):

<?php
if ( function_exists(‘register_sidebar’) )
register_sidebar();
?>

Some of mine have some more lines after  register_sidebar();, and this is exactly where you should add your code. It doesn’t matter what order the various functions are in, so just make a new line afterregister_sidebar(); and paste in the additional code. My Themes Function file on this site looks like this. The stuff that was already in there is in regular font; the stuff I added is bolded.

<?php
if ( function_exists(‘register_sidebar’) )
register_sidebars(2,array(
‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’,
));
remove_action(‘wp_head’, ‘rsd_link’);
remove_action(‘wp_head’, ‘wlwmanifest_link’);
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);
function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed

if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text, ‘<p>’);
$excerpt_length = 75;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘ … <a href=”‘. get_permalink() . ‘”>READ MORE</a>’);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

?>

That’s it!

Related posts:
  1. What’s this weird page in my Wordpress theme?
  2. Theme test drive Wordpress plugin
  3. If the styling for captions in Wordpress 2.6 doesn’t seem to work…
  4. SEO tip for Wordpress themes
  5. Curl code for file inclusion

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>