Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I override WP's 'excerpt_more' filter via my child theme functions?

I can't seem to get my functions to work for changing the excerpt_more filter of the Twenty Eleven parent theme.

I suspect it might actually be add_action( 'after_setup_theme', 'twentyeleven_setup' ); that's the problem, but I've even tried remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' ) to get rid Twenty Eleven's function and still my functions aren't changing anything...

Can you help?

Here's the functions.php code in full:

http://pastie.org/3758708

Here's the functions I've added to /mychildtheme/functions.php

function clientname_continue_reading_link() {
    return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Read more... <span class="meta-nav">&rarr;</span>', 'clientname' ) . '</a>';
}
function clientname_auto_excerpt_more( $more ) {
    return ' &hellip;' . clientname_continue_reading_link();
}
add_filter( 'excerpt_more', 'clientname_auto_excerpt_more' );

Thanks,

Osu

like image 534
Osu Avatar asked Dec 10 '22 01:12

Osu


2 Answers

Ok, so after much frustration, I found a solution to this (I thought Child Themes were meant to speed things up!?). I believe this works because 'after_theme_setup' is run once the parent theme has been set up meaning you can remove / override Twenty Eleven's functions at that point.

If I've understood correctly, according to this documentation, the Child Theme is run first, then the parent and then the 'after_theme_setup' bit of code in your Child Theme's functions.php file:

http://codex.wordpress.org/Child_Themes#Using_functions.php

and

http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme

This is what's in my Child Theme's functions.php file, hope this helps someone:

// ------------------------------------------------------------------
//                      // !AFTER_SETUP_THEME
// ------------------------------------------------------------------

/* Set up actions */
add_action( 'after_setup_theme', 'osu_setup' );

if ( ! function_exists( 'osu_setup' ) ):

function osu_setup() {

    // OVERRIDE : SIDEBAR GENERATION FUNCTION - NO WIDGETS FOR THIS SITE
    remove_action( 'widgets_init', 'twentyeleven_widgets_init' ); /* Deregister sidebar in parent */

    // OVERRIDE : EXCERPT READ MORE LINK FUNCTION
    function osu_readon_link() {
        return '...<a href="'. get_permalink() . '" class="readmore">' . __( 'Read More...', 'clientname' ) . '</a>';
    }
    // Function to override
    function osu_clientname_custom_excerpt_more( $output ) {
        if ( has_excerpt() && ! is_attachment() ) {
            // $output = trim($output);
            $output .= osu_readon_link();
        }
        return $output;
    }
    remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    add_filter( 'get_the_excerpt', 'osu_clientname_custom_excerpt_more' );
    remove_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
    add_filter( 'excerpt_more', 'osu_readon_link' );

    // OVERRIDE : EXCERPT LENGTH FUNCTION
    function osu_clientname_excerpt_length( $length ) {
        return 30;
    }
    remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );
    add_filter( 'excerpt_length', 'osu_clientname_excerpt_length' );

}
endif; // osu_setup
like image 88
Osu Avatar answered Mar 02 '23 02:03

Osu


Your own answer is complicating things and really shouldn't be necessary. I couldn't explain the why of my answers, because I found it in another answer. But in any case you can ALWAYS override functions from the parent theme in your child theme, although sometimes indeed you to use the remove_filter() beforehand, OR, like in this case, all you have to do is increase the priority of your added filter, in your case:

add_filter( 'excerpt_more', 'clientname_auto_excerpt_more', 11 );

That should do the trick. If not, increase the number. Thanks to this answer:

like image 40
Ralph Smit Avatar answered Mar 02 '23 02:03

Ralph Smit