[wp-testers] apply_filters()

Jennifer Hodgdon yahgrp at poplarware.com
Tue May 26 17:11:10 GMT 2009


That all looks fine, so far... but who is calling the 
KEW_short_excerpt function, and what is the calling function doing 
with the output?

Also, this whole discussion belongs on the wp-hackers list, not 
wp-testers... just noticed where it was...

    --Jennifer

-------- Original Message --------
Subject: Re: [wp-testers] apply_filters()
Date: Tue, 26 May 2009 18:39:18 +0200
From: Karl Wångstedt <maillist at wangstedt.net>
Reply-To: wp-testers at lists.automattic.com
To: wp-testers at lists.automattic.com
References: <77AC2B5B-F81E-4003-A58E-6CFDF45FE1A6 at wangstedt.net> 
<4A1BF795.6000605 at poplarware.com>


26 maj 2009 kl. 16.07 skrev Jennifer Hodgdon:

> Maybe if you posted some of your code, we could see what is going  
> wrong? You can definitely apply filters without echoing the result,  
> to make something "filterable" in your theme... Example:
>
> a) In your parent theme, do something like this:
>
> $myvariable = apply_filters( 'my_filter_name', $myvariable );
>
> b) In a plugin or subtheme's functions.php, do something like this:
>
> add_filter( 'my_filter_name', 'my_filter_func' );
>
> function my_filter_func( $input ) {
>   $input .= ' additional text added to end'; // or other filtering
>   return $input;
> }


That is exactly what I did, but still the filter only gets applied
when I echo the result.

This is the parent theme function:

function KEW_short_excerpt($text = '', $max_length = 100, $preserve =
'<p>', $replace = '', $replacement = '')
{
	global $post;

	$text = trim($text);

	if ( $text == '' )
	{
		$content = $post->post_content;
		
		$excerpt = apply_filters('the_content', $content);		

		$excerpt = trim(strip_tags($excerpt,$preserve.' '.$replacement)); //
Rensa bort formatering förutom de taggar som listas

		$words = preg_split("/(?<=(\.|!|\?)+)\s/", $excerpt, -1,
PREG_SPLIT_NO_EMPTY);

		foreach ( $words as $word )
		{
			$new_text = $text . substr($excerpt, 0, strpos($excerpt, $word) +
strlen($word));
			$excerpt = substr($excerpt, strpos($excerpt, $word) +
strlen($word), strlen($excerpt));

			if ( ( strlen($text) != 0 ) && ( strlen($new_text) > $max_length ) )
			{
				$text .= " [...]";
				break;
			}

			$text = $new_text;
		}
	
	
	}
	// Se till att öppnade taggar avslutas (kräver att WP är inställt
på att automatiskt korrigera felaktig XHTML
	$text = balanceTags($text, 0, true);
	$text = wpautop($text); // Kolla så att alla taggar ärstängda och
att de verkligen finns.
	
	// För att länka all text i excerpten till posten
	//$text = preg_replace('{(<(p|h[1-6])[^>]*>)([^<>]*?)(</(p|
h[1-6])>)}','\1<a href="'.get_permalink($post->ID).'">\3</a>\4',$text);

	
		// Här byter vi ut taggar i excerpten mot en ny ($replacement)
		// Den tagg som ska bytas måste finnas i $preserve i templaten för
att det ska funka
		if($replace && $replacement) {
			$replacement_tag = substr($replacement,1,2); // Bryt ut hn ur <hn>
från taggen som ska vara kvar
			$replacement_end = '</'.$replacement_tag.'>'; // Skapa
avslutningstagg
			$replace_tag = substr($replace,1,2); // Bryt ut hn ur <hn> från
taggen som ska bort
			$replace_end = '</'.$replace_tag.'>'; // Skapa avslutningstagg
			
			$text = str_replace($replace, $replacement, $text); // Byt ut
starttaggarna
			$text = str_replace($replace_end, $replacement_end, $text); // Byt
ut sluttaggarna
		}
	
	echo apply_filters('_short_excerpt',$text);
	//return apply_filters('_short_excerpt',$text);
}

The two last rows is what I'm toggling between. If I use return the
filter doesn't gets applied, but if I use echo it does.

This is what's in the child theme:

function KEW_add_cat_name_to_short_excerpt($text=''){
	global $post;
	
	$categories = get_the_category($post->ID);
	$name = $categories[0]->cat_name;
		$text = preg_replace('/<p>/','<p><span class="run-in">▸ '.$name.'</
span>&nbsp;',$text); // KEW_short_excerpt
		
	return $text;

}
add_filter('_short_excerpt','KEW_add_cat_name_to_short_excerpt');

-- 
Jennifer Hodgdon * Poplar ProductivityWare
www.poplarware.com
Drupal, WordPress, and custom Web programming



More information about the wp-testers mailing list