Customizing template files inside inc folders while using a child theme

So the title pretty much sums it up; I want to customize some template files in the inc/ folder while using a child theme. Unfortunately, changes are not registered even when using the same file structure (which usually works with other themes and plugins like woocommerce).

Example for clarity: I want to modify the file in Leto-child/inc/header-functions.php
Change


  • <?php esc_html_e( 'Login / Register', 'leto' ); ?>
  • to


  • <?php esc_html_e( 'HELLO', 'leto' ); ?>
  • this is just an example, please don’t tell me how to change the login text with javascript or similiar

    So, is it possible to customize the theme without touching the parent theme and losing everything with the next update?

    Thanks

    Hello there,

    Child theme will still load parent’s functions inside the inc folder. Its inc folder won’t be loaded as it should be manually linked with get_stylesheet_directory_uri() in the main functions.php file. The ideal way to override functions which are hooked with add_action(), is to exclude the paren’t function with this PHP code:

    /**
     * Remove Leto Main Navigation
     */
    
    add_action('init', 'leto_child_remove_preloader');
    function leto_child_remove_preloader() {
      remove_action( 'leto_inside_header', 'leto_main_navigation', 9 );
    }
    

    Add it to child theme’s functions.php file.

    See this codex: https://codex.wordpress.org/Function_Reference/remove_action

    Then you can override the old function by adding this code to child theme’s functions (functions.php).

    /**
     * Re-create Leto Main Navigation
     */
    
    add_action('leto_inside_header', 'leto_child_main_navigation', 11);
    function leto_child_main_navigation() {
      ?>
    		<nav id="site-navigation" class="main-navigation">
    			<?php
    				wp_nav_menu( array(
    					'theme_location' => 'menu-1',
    					'menu_id'        => 'primary-menu',
    				) );
    			?>
    		</nav><!-- #site-navigation -->
    
    		<div class="header-mobile-menu">
    			<div class="header-mobile-menu__inner">
    				<button class="toggle-mobile-menu">
    					<span><?php esc_html_e( 'Toggle menu', 'leto' ); ?></span>
    				</button>
    			</div>
    		</div><!-- /.header-mobile-menu -->
    
    
    		<?php $show_menu_additions = get_theme_mod( 'leto_show_menu_additions', 1 ); ?>
    		<?php if ( $show_menu_additions ) : ?>
    		<ul class="nav-link-right">
    			<li class="nav-link-account">
    				<a href="<?php echo esc_url( wp_login_url() ); ?>" title="Login"><span class="prefix"><?php esc_html_e( 'Login / Register', 'leto' ); ?></span> <span class="suffix ion-person"></span></a>
    			</li>
    
    
    			<?php if ( class_exists( 'Woocommerce' ) ) : ?>
    
    			<?php $cart_content = WC()->cart->cart_contents_count; ?>
    
    			<li class="nav-link-cart">
    				<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="header-cart-link">
    					<i class="ion-bag"></i>
    					<span class="screen-reader-text"><?php esc_html_e( 'Cart', 'leto' ); ?></span>
    					<span class="cart-count">(<?php echo intval($cart_content); ?>)</span>
    				</a>
    				<div class="sub-menu cart-mini-wrapper">
    					<div class="cart-mini-wrapper__inner">
    					<?php woocommerce_mini_cart(); ?>
    					</div>
    				</div>
    			</li>
    			<?php endif; //end Woocommerce class_exists check ?>
    
    			<?php
    			$enable_search = get_theme_mod( 'leto_enable_search', 1 );
    			if ( $enable_search ) : ?>
    			<li class="nav-link-search">
    				<a href="#" class="toggle-search-box">
    					<i class="ion-ios-search"></i>
    				</a>
    			</li>
    			<?php endif; ?>
    
    		</ul>
    		<?php endif; ?>
    
    	<?php
    }
    

    Then find this line of code:

    <?php esc_html_e( 'Login / Register', 'leto' ); ?>
    

    and replace the text string with any of your choice.

    Regards,
    Kharis

    Works like a charm, Thank you!

    Although I found it easier if I just comment out the

    require get_template_directory() . '/inc/header-functions.php';

    part in the parent functions.php then use

    require get_theme_file_path( 'inc/header-functions.php' );

    in the child functions.php.

    It’s more organized to edit the whole template file, rather than removing and recreating every part and having a chaotic functions.php file.

    Great! You’re welcome!

    When new version update notification coming in the future, you’ll need to comment out the one in your child theme. Because all files will be overridden after updating, which is resulting function duplication. Comment out a line in parent’s functions means modifying it, which is not recommended. Please be advised.

    Regards,
    Kharis