0

I'm having trouble using remove_filter to override the read more links in my parent theme.

Here's the parent theme functions:

/* Modify the '[...]' Read More Text */
add_filter( 'the_content_more_link', 'hoot_modify_read_more_link' );
if ( apply_filters( 'hoot_force_excerpt_readmore', true ) ) {
    add_filter( 'excerpt_more', 'hoot_insert_excerpt_readmore_quicktag', 11 );
    add_filter( 'wp_trim_excerpt', 'hoot_replace_excerpt_readmore_quicktag', 11, 2 );
} else {
    add_filter( 'excerpt_more', 'hoot_modify_read_more_link' );
}

/**
 * Modify the '[...]' Read More Text
 *
 * @since 1.0
 * @access public
 * @return string
 */
function hoot_modify_read_more_link( $more = '[…]' ) {
    if ( is_admin() )
        return $more;

    $read_more = esc_html( hoot_get_mod('read_more') );
    $read_more = ( empty( $read_more ) ) ? sprintf( __( 'Read More %s', 'brigsby' ), '→' ) : $read_more;
    global $post;
    $read_more = '<a class="more-link" href="' . esc_url( get_permalink( $post->ID ) ) . '">' . $read_more . '</a>';
    return apply_filters( 'hoot_readmore', $read_more ) ;

}

And here's my functions in my child theme's function.php:

function dw_remove_parent_read_more() {
    remove_filter('excerpt_more', 'hoot_modify_read_more_link');
}
add_action('after_setup_theme', 'dw_remove_parent_read_more',999);

function dw_modify_read_more_link($more = '[&hellip;]') {

    if ( is_admin() )
        return $more;

    $read_more = esc_html( hoot_get_mod('read_more') );
    $read_more = ( empty( $read_more ) ) ? sprintf( __( 'Read More %s', 'brigsby' ), '' ) : $read_more;
    global $post;
    $read_more = '<div><a class="more-link" href="' . esc_url( get_permalink( $post->ID ) ) . '">' . $read_more . '</a></div>';
    return apply_filters( 'hoot_readmore', $read_more ) ; 
}
add_filter('excerpt_more', 'dw_modify_read_more_link', 999);

The current result is that two read more links are now showing, both the parent theme's and my child theme's. What am I doing wrong :) ?

2 Answers 2

2

A Note on Priorities

Note that if you're trying to remove a function using remove_action() or remove_filter() and the function has had a priority assigned to it, you must include the priority when removing it, or it won't work.

So if the function in the parent theme looks like this:

<?php
function parent_function() {
    // Contents for your function here.
}
add_action( 'init', 'parent_function', 15 );
?>
<?php
function child_remove_parent_function() {
    remove_action( 'widgets_init', 'parent_function', 15 );
}
add_action( 'wp_loaded', 'child_remove_parent_function' );
?>
. 

you'll need to include the same priority value when removing it:

0

Figured it out, the 'Read More' link being displayed was from the wp_trim_excerpt hook rather than excerpt_more hook. Just needed to switch my add_filter and remove_filter to reference the appropriate hooks.

function dw_remove_parent_read_more() {
    remove_filter('wp_trim_excerpt', 'hoot_replace_excerpt_readmore_quicktag', 11, 2);
}
add_action('after_setup_theme', 'dw_remove_parent_read_more',11);

function dw_replace_excerpt_readmore_quicktag( $text, $raw_excerpt ) {
    if ( is_admin() )
        return $text;
    $read_more = dw_modify_read_more_link();
    $text = str_replace( '<!--hoot-read-more-quicktag-->', '', $text );
    return $text . $read_more ;
}

add_filter('wp_trim_excerpt', 'dw_replace_excerpt_readmore_quicktag', 11,2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.