I would like to include line breaks on my WordPress excerpts.
To accomplish this, I see that I can change this function:
function wp_strip_all_tags($string, $remove_breaks = false) { $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); $string = strip_tags($string); if ( $remove_breaks ) $string = preg_replace('/[\r\n\t ]+/', ' ', $string); return trim( $string ); } to:
function wp_strip_all_tags_breaks($string, $remove_breaks = false) { $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); $string = strip_tags($string, '<p>'); if ( $remove_breaks ) $string = preg_replace('/[\r\n\t ]+/', ' ', $string); return trim( $string ); } What is the best way to modify my theme to switch functions and provide this functionality?
21 Answer
Overriding/overloading any of the WordPress core functions has to be done in the functions.php of your current theme.
First you have to define the new function in the functions.php (the name should be different from the original wpcore function name) and then you have remove the old function and add the new function to the respective hook/filter.
In case of the_excerpt() it should be done like this:
function new_function() { //code here } remove_filter('get_the_excerpt', 'old_function'); add_filter('get_the_excerpt', 'new_function'); Hope that makes sense.
EDIT: Here is a good tutorial on how to edit the_excerpt() formatting.