[wp-hackers] apply_filters()

Alan Castonguay alan at verselogic.net
Thu May 28 00:21:16 GMT 2009


Karl,

This might depend on how you were calling KEW_short_excerpt(); were  
you actually echoing the result? Try tracing through the following.

http://blog.verselogic.net/?karl=1


         function KEW_short_excerpt($text = '', $max_length = 100,  
$preserve = '<p>', $replace = '', $replacement = ''){
                 // The function
                 echo "Before filter: $text\n";
                 $c = apply_filters('_short_excerpt',$text);
                 echo "After filter: $c\n";
                 return $c;
         }

         function KEW_add_cat_name_to_short_excerpt($text=''){
                 echo "Filter called with ($text)\n";
                 $text = preg_replace('/cat/','DOG',$text);
                 echo "Filter returning ($text)\n";
                 return $text;
         }
         echo "<pre>Calling KEW_short_excerpt('The lazy cat sleeps.') 
\n";
         $a = KEW_short_excerpt('The lazy cat sleeps.');
         echo " - returned ($a)\n\n";
         echo "Adding filter.\n";
          
add_filter('_short_excerpt','KEW_add_cat_name_to_short_excerpt');
         echo "Calling KEW_short_excerpt('The lazy cat sleeps.')\n";
         $b = KEW_short_excerpt('The lazy cat sleeps.');
         echo " - returned ($b)\n";
         echo "</pre>";


Alan J Castonguay



On May 26, 2009, at 4:00 PM, Karl Wångstedt wrote:

> Hi all
>
> I'm trying to make a function in my parent theme fiterable (is that  
> a correct description?) by apply_filters on the return value.
>
> But now it seems like I can only add_filter() to the result if it  
> gets echoed from the function. Now I'd like to just return the  
> result for use in PHP, but then the add_filter() isn't applied.
>
> Is it so, or am I doing something wrong?
>
> This is what my functions look like (a bit stripped):
> // In the parent theme
> function KEW_short_excerpt($text = '', $max_length = 100, $preserve  
> = '<p>', $replace = '', $replacement = ''){
> 	// The function
>
> 	echo apply_filters('_short_excerpt',$text);
> 	//return apply_filters('_short_excerpt',$text);
> }
>
> // 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');
>
> It's the two last rows in the parent theme function that I toggle  
> between. The filter gets applied when I use echo, but not if I use  
> return.
>
>
>
> Thank you
> Karl Wångstedt
> ----------------------------------
>        Wångstedt  nova
>       www.wangstedt.net
>
> Karl Wångstedt       0278-61 12 33
> Stenhuggarvägen 3g   070-578 13 04
> 821 41  BOLLNÄS
> ----------------------------------
> Det här brevet skickades med en Macintosh från Apple.
> Garanterat virusfri med Mac OS X.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list