affiliater affiliater - 2 months ago 36
PHP Question

Override themes functions

In my theme there is a functions-template.php file with a lot of functions. One of them echoes the category description on the site.

function woocommerce_taxonomy_archive_description() {
if ( is_tax( array( 'product_cat', 'product_tag' ) ) && get_query_var( 'paged' ) == 0 ) {
global $wp_query;

$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'full' );

$description = apply_filters( 'the_content', term_description() );

if ( $image && yit_get_option( 'shop-category-image' ) == 1 ) {
echo '<div class="term-header-image"><img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[1] . '" alt="' . $cat->name . '" /></div>';

if ( $description ) {
echo '<div class="term-description">' . $description . '</div>';

I want to echo another variable instead without messing with the files. Is there a way to "override" an existing function?

I've been fiddling a bit with mu-plugins etc but with no success.
I always get the
Fatal error: Cannot redeclare woocommerce_taxonomy_archive_description() (previously declared in
error when adding the same function in my custom functions file..


Yes it can be overridden from a child theme. You can override the function from your child themes functions.php file.

See more about child theme

WordPress loads child theme first and then the parent theme. So if you create a function with same name in your child theme, then the if condition with ! function_exists will will be false and hence there this function won't be declared.

If you want to override from a plugin then you've to declare the function in earlier execution. Try declaring it in a init hook.

    add_action('init', 'theme_func_override', 5);
    function theme_func_override(){
        function override_func(){
            //code goes here


If the function isn't declared with function_exists() check within a if condition then you can't override it!