phpwordpresshookadd-filter

Override part in WordPress parent theme with filter


Not sure if this is overly possible to do, but I'm trying to override a small part in a parent theme's template_tags.php file. I've tried to copy the same file over to my child theme, but it doesn't seem to be overriding the file. So I'm wondering if I need to use a filter to get it to change, but I'm not sure how to go about it.

This is what I'm trying to change:

            <?php if(has_header_image()){?>

            <?php
            $headerImage = get_header_image();
            if(class_exists('woocommerce')){
                if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
                    $thumbId = get_post_thumbnail_id();
                    $thumbImage = wp_get_attachment_url( $thumbId );
                }

                if(!empty($thumbImage)){
                    $headerImage = $thumbImage;
                }
            }
            ?>
            <div class="rellax">
                <img src="<?php echo esc_url( $headerImage ); ?>">
            </div>
            <?php } ?>

to something like this:

<?php if(has_header_image()){?>

                <?php
                $headerImage = get_header_image();
                if(class_exists('woocommerce')){
                    if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
                        $thumbId = get_post_thumbnail_id();
                        $thumbImage = wp_get_attachment_url( $thumbId );
                    }

                    if(!empty($thumbImage)){
                        $headerImage = $thumbImage;
                    }
                }
                ?>
                <div class="rellax">
                    <?php if ( has_post_thumbnail() ): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else : ?>
                        <img src="<?php echo esc_url( $headerImage ); ?>">
                    <?php endif; ?>
                </div>
            <?php } ?>

Overall all I'm really trying to add/update is this part:

<div class="rellax">
                    <?php if ( has_post_thumbnail() ): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else : ?>
                        <img src="<?php echo esc_url( $headerImage ); ?>">
                    <?php endif; ?>
                </div>

I know it seems like overkill for such a small thing, but I don't want to modify the file directly within the parent theme incase there is an update to the file and it blows out any changes that I've added.

Again, not sure if it's possible or the best approach would be.

Here's the full function that it's wrapped in:

if( !function_exists('business_idea_breadcrumbs')){
    function business_idea_breadcrumbs(){
        $business_idea_option = wp_parse_args(  get_option( 'business_idea_option', array() ), business_idea_default_data() );
        ?>
        <section class="sub-header">

            <?php if(has_header_image()){?>

                <?php
                $headerImage = get_header_image();
                if(class_exists('woocommerce')){
                    if( is_woocommerce() || is_product() || is_cart() || is_shop() ){
                        $thumbId = get_post_thumbnail_id();
                        $thumbImage = wp_get_attachment_url( $thumbId );
                    }

                    if(!empty($thumbImage)){
                        $headerImage = $thumbImage;
                    }
                }
                ?>
                <div class="rellax">
                    <?php if ( has_post_thumbnail() ): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else : ?>
                        <img src="<?php echo esc_url( $headerImage ); ?>">
                    <?php endif; ?>
                </div>
            <?php } ?>

            <?php if(has_header_image()){?>
                <div class="sub-header-overlay">
                <?php } ?>
                <div class="container sub-header-container">
                    <div class="row">
                        <div class="col-md-12 text-center">
                            <?php
                            if( function_exists('bcn_display') ){
                                bcn_display();
                            }else if( is_front_page() && is_home() ){
                                echo '<h1 class="page_title">' . __('Home','business-idea') . '</h1>';
                            }else if( is_archive() ){
                                the_archive_title( '<h1 class="page_title">', '</h1>' );
                                the_archive_description( '<div class="taxonomy-description">', '</div>' );
                            }else{
                                ?>
                                <h1 class="page_title"><?php single_post_title(); ?></h1>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <?php if(has_header_image()){?>
                </div>
            <?php } ?>
        </section>
        <?php
    }
}

Solution

  • As stated in my comment, applying a filter would only work if there would be an according "apply_filter" call in the original code for the exact thing you're trying to change. In this case, you'll have to override the complete function by copying it into your child theme and removing the

    if( !function_exists('business_idea_breadcrumbs')){
    

    plus the last closing bracket.

    This way, the function will exist when the parent function runs into the function_exists check. Then just change the function in the child according to your needs.