Back to top
Add useful functions in WordPress
  • May 16th 2010

You can find these extra functions on the WordPress forums and all over the internet, but let’s share what I use and works for me.

Adding extra widget areas in WordPress.
[

Add Extra Widgets

Add Extra Widgets

I will show you a quick and easy way to add extra widget areas in wordpress.
I went from 1 to 7 widgets for my theme, and it helped me alot. So whenever I have to change something, I can just do it from my admin widget area.

You’ll have to register your sidebar so that wordpress recognizes it.

* Let’s see how this function looks like:

	register_sidebar(array(
		'name' => 'Sidebar TopRight',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="widgettitle">',
		'after_title' => '</h4>',
	));

Put this in the theme file functions.php. This will display it in your admin’s widget area.
You can change the div and h4 tags. Just adapt the one from your own sidebar.

* The code to actualy display the widget on your website:

<?php dynamic_sidebar('[Sidebar TopRight'); ?>

You can place this code in your sidebar.php, footer.php, header.php or any other file where you like your new widget to appear.

Now upload both of your files and login to the admin area to add a new widget.
That's it!

------------------------------------------

Adding Post-Thumbnail support to your themes.

Add Post Thumbnails

Add Post Thumbnails

This is the best solution for my theme, because I needed exact dimensions for my articles.
It uses the built in dimensions in the Settings-->Media area. This will only work in WordPress 2.9 and up!

* This goes in the functions.php file:

// Add & configure post thumb support
define('THUMB_WIDTH', get_option('thumbnail_size_w'));
define('THUMB_HEIGHT', get_option('thumbnail_size_h'));
add_theme_support( 'post-thumbnails' );
// Set post thumb dimensions to the values entered in Settings/Media
if ( function_exists( 'set_post_thumbnail_size' ) ) {
	set_post_thumbnail_size( THUMB_WIDTH, THUMB_HEIGHT ); // box (proportional) resize mode
}

Then in your index.php or single.php, were you want the thumbnails displayed:

<?php if( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>

If your thumbnails have slightly different sizes, to get exact dimensions use this in your index.php or single.php:

<?php if( function_exists( 'add_theme_support' ) ) the_post_thumbnail( array(150,100) ); ?>

Replace, the width, 150 and the height, 100, with your own values.

There will be an "Add thumbnail" on the right side of your add/edit post screen were you can choose a thumbnail from your medialibrary, NextGen Gallery or even upload a new image.

It only works fully for new image uploads.
Regenerate Thumbnails is a perfect plugin from Viper007Bond to regenerate all your thumbnails after changing the thumbnail sizes.

There are plugins you can use to do this for you. Like the one from Fairweb. I haven't test it.

--------------------------------------------

Truncate/trim long titles.

Truncate/Trim Long Titles

Truncate/Trim Long Titles

If you have page were you want your long titles trimmed then this code worked for me.
Put this in your functions.php file:

// truncate/trim long titles
function short_title($after = '', $length) {
	$mytitle = get_the_title();
	if ( strlen($mytitle) > $length ) {
	$mytitle = substr($mytitle,0,$length);
	echo $mytitle . $after;
	} else {
	echo $mytitle;
	}
}

Now replace your

<?php the_title();?>

with

<?php short_title('...', 22); ?>

22 is the character lenght. change this to whatever lenght you want to use.

---------------------------------------------

Add automatic external link icon to outside links.

External Link Icon

External Link Icon


In your functions.php:

//automatic external link icon with class="external"
function autoicon($text) {
$return = str_replace('<a href=', '<a class="external" href=', $text);
$return = str_replace('<a class="external" href="http://www.domain.com', '<a href="http://www.domain.com', $return);
$return = str_replace('<a class="external" href="#', '<a href="#', $return);
return $return;
}
add_filter('the_content', 'autoicon');
add_filter('comment_text', 'autoicon');

The CSS class to put in your style.css theme file:

.external {
background:url(http://www.yourwebsite.com/ext.png) center right no-repeat;
padding-right:14px;
}

What this does is look for any urls that isn't your own (internal links) and marks it with class="external".
This will show your visitors which links goes outside your website.
You need to be consistent in your internal links, so if you never use www on your links then remove the www., so it becomes,

  http://yourwebsite.com', '<a href="http://yourwebsite.com'

That's how simple adding extra functions to your WordPress powered site is.


This post is tagged , ,

Possibly Related posts:

  1. Then And Now Recharged And again there have been some changes to the site’s...
  2. Then And Now Reloaded Since the last time I changed the overal look of...
  3. Useful attachment image codes Sometimes it’s hard to find help on the WordPress forums...