Allowing Hyperlinks in Your WordPress Excerpts

September 22nd, 2010  |  Published in Mind

By default, WordPress strips out all the HTML tags from your post excerpts. I needed to allow hyperlinks, but there is a problem when WordPress tries to truncate the post’s content. The wp_trim_excerpt function is what WordPress uses to do all the trimming work, I simply copied the code, modified it, and stuck my new version in the mu-plugins directory (or you could add it to your functions.php).

The first line I needed to change was the PHP function call strip_tags(). I need to set it to allow the <a> tag… very simple to do:

Original Line:

$text = strip_tags($text);

New Line:

$text = strip_tags($text, '<a>');

The second line I needed to change was where WordPress splits the excerpt up into separate words. This was a bit tricky because you run into an issue where you could be cutting of the </a>, which could screw up your entire page. I decided the best thing to do would be to treat the entire hyperlink as a single word. So, <a href=”http://lewayotte.com/”>My Website</a>, would count as a single word. By default, WordPress creates a 55 word excerpt. That is pretty easy to change as well.

Original Line:

$words = preg_split('/[\n\r\t ]', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );

New Line:

$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );

Next, you simple need to remove the current wp_trim_excerpt filter and add a new one that points to your new function. Here is what the entire block of code looks like for me:

<?php
function new_wp_trim_excerpt($text) {
	$raw_excerpt = $text;
	if ( '' == $text ) {
		$text = get_the_content('');

		$text = strip_shortcodes( $text );

		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$text = strip_tags($text, '<a>');
		$excerpt_length = apply_filters('excerpt_length', 55);

		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
		$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
		if ( count($words) > $excerpt_length ) {
			array_pop($words);
			$text = implode(' ', $words);
			$text = $text . $excerpt_more;
		} else {
			$text = implode(' ', $words);
		}
	}
	return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);

}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'new_wp_trim_excerpt');

Regular Expression Explained

I changed /[\n\r\t\s ]/ to /(<a.*?a>)|\n|\r|\t|\s/ because I needed to capture everything in the <a> HTML tags and count it as a single word. \n, \r, \t, \s are all pretty basic regex characters that preg_split() is using to break up the content. (<a.*?a>) is what captures everything in the <a> tag. The .* means all “characters” adding the ? to .* makes it “non-greedy.” This for the case where there are multiple hyperlinks in the content. It prevents the regex from thinking <a href=”link1″>link 1</a>, <a href=”link2″>link 2</a> is a single word. The parenthesis simply group the <a.*?a> together, so it doesn’t try to split those items separately and then I needed to add PREG_SPLIT_DELIM_CAPTURE to preg_split, or preg_split would have just removed the link as garbage.

You should be able to use this as a pretty solid base for adding other HTML tags.

Tags: , , , , , , ,

Embed an RSS/ATOM Feed into your WordPress Website

April 26th, 2010  |  Published in Mind

One of my clients needs to pull an RSS feed from a real estate virtual tour provider and display it on one of their pages. I looked into a few plugins and could not find one that was easily customizable. After a quick google search I discovered that WordPress has some built-in functions based off of SimplePie‘s easy to use RSS API.

I also needed to customize the output a little bit, which required me to rely on PHP’s preg_match and preg_replace functions. Here is some simple example for embedding a feed into your WordPess website:

<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_feed('http://url.to.feed/'); // specify feed url
?>

<?php
if (!is_wp_error($feed)) :
	$rss_items = $feed->get_items();
?>
    // Loop through each feed item and display each item as a hyperlink. <?php foreach ( $rss_items as $item ) : ?>
  • <?php echo $item->get_title(); ?>
  • <?php endforeach; ?>

Check out the codex for more information about WordPress’ function feed_fetch()… and with a little extra coding you can format the feed any way you like, see our example at NC Capital Homes.

Tags: , ,

Easily Extended Contact Info in WordPress

April 22nd, 2010  |  Published in Mind

I’m working on a project where I need the ability to add “Phone”, “Building”, and “Room” as information a user could add in their profile. I also wanted to remove “AIM”, “Yahoo”, and “Jabber” — none of the users are going to need to fill in that info.

You can easily add this code into your functions.php. I was writing it for a WordPress MU site with multiple themes and needed to make sure it was enforced, so I stuck it in the mu-plugins directory, in a file named “extended-contact-info.php”.

This is the simple code block you need:

function extended_contact_info($user_contactmethods) {
	//Use this if you want to append to the default
	//contact methods (AIM, Yahoo, Jabber)
	//$user_contactmethods += array(
	//	'building' => __('Building'),
	//	'room' => __('Room'),
	//	'phone' => __('Phone')
	//);

	//Use this if you want to ignore the default
	//contact methods (AIM, Yahoo, Jabber)
	$user_contactmethods = array(
		'building' => __('Building'),
		'room' => __('Room'),
		'phone' => __('Phone')
	);

	return $user_contactmethods;
}

add_filter('user_contactmethods', 'extended_contact_info');

Tags: , , ,

Notable Tech Posts – 2010.01.17

January 17th, 2010  |  Published in Mind

TiptTp jQuery plugin

Create a PHP  server load meter

jQuery Events: MouseOver – MouseOut vs MouseEnter – MouseLeave

15 useful jQuery plugins and tutorials

CSS important

Sexy jQuery drop down multi-level menu

jQuery quick guide

Take your HTML tables to a new level with Javascript frameworks

Javascript replace all using regex for

Tags: , , , , , , , , ,

Notable Tech Posts – 2010.01.10

January 10th, 2010  |  Published in Mind

Getting started with jQuery

Free furry cushions social icons set

WordPress shopping cart plugins

Converting your PHP app to MySQLi prepared statements

Autosuggest jQuery plugin

jQuery validation with regular expressions

Extending CSS with jQuery a new years guide

CSS 3D meninas -  just something cool to look at, pure CSS/HTML

jQuery animations a 7-step program

Implement password strength meter gauge JS

Colorful clock jQuery CSS

The best of the best jQuery resources

Cheat sheet series Javascript

MySQL: Using if in where

Tags: , , , , , , , ,