Adding Child Theme filters and Twenty Ten

Working on a child theme project for a client I found that my filter modifications for both ‘excerpt_length’ and ‘excerpt_more” were not being processed. My child theme is based on Twenty Ten and the filters I was using had already been filtered by the parent theme. So, I had added my custom filters and the Twenty Ten filter removal code to my child themes functions.php.

However, these custom filters were not being actioned.┬áTo help me debug what was happening I even added PHP error logging to check the return values from the remove_filter functions and that proved the filters were not being removed prior to mine being added but I still couldn’t work out why.

Finally by searching the WordPress forums I came across this post and read the reply from lordarkad. It made sense. In my child theme I had to wait for the parent themes setup to finish before declaring my filter changes and this made more sense because the child themes functions.php is loaded prior to the parent themes functions.php. So whilst these filters showed in the parent theme code I was removing them before they had been applied and, of course, then the parent themes functions.php was applying it’s filters in the theme.

To fix it I followed lordarkad‘s comments and created my filter additions in a child theme setup function. Then I added that function to the “after_setup_theme” action so my function is called after the parent theme has complete setup and then my filters are applied correctly.

This is a snippet of what I ended up with:

add_action( 'after_setup_theme', 'mytheme_theme_setup' );

function mytheme_theme_setup() {
	if (!function_exists('mytheme_new_excerpt_length') ) {
		function mytheme_new_excerpt_length( $length ) {
			return 20;
		}
	}
	remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
	add_filter( 'excerpt_length', 'mytheme_new_excerpt_length' );
etc...
} // end of mytheme_theme_setup

Leave a Reply

Your email address will not be published. Required fields are marked *