• In template-tags.php in parent theme, I found such code

    add_action( ‘onepress_site_start’, ‘onepress_site_header’ );
    if ( ! function_exists( ‘onepress_site_header’ ) ) {
    /**
    * Display site header
    */
    function onepress_site_header(){
    $header_width = get_theme_mod( ‘onepress_header_width’, ‘contained’ );
    $is_disable_sticky = sanitize_text_field( get_theme_mod( ‘onepress_sticky_header_disable’ ) );
    $classes = array(
    ‘site-header’, ‘header-‘.$header_width,
    );

    if ( $is_disable_sticky != 1 ) {
    $classes[] =’is-sticky no-scroll’;
    } else {
    $classes[] =’no-sticky no-scroll’;
    }

    $transparent = ‘no-t’;
    if ( onepress_is_transparent_header() ){
    $transparent = ‘is-t’;
    }
    $classes[] = $transparent;

    $pos = sanitize_text_field(get_theme_mod(‘onepress_header_position’, ‘top’));
    if ($pos == ‘below_hero’) {
    $classes[] = ‘h-below-hero’;
    } else {
    $classes[] = ‘h-on-top’;
    }

    ?>
    <header id=”masthead” class=”<?php echo esc_attr( join(‘ ‘, $classes ) ); ?>” role=”banner”>
    <div class=”container”>
    <div class=”site-branding”>
    <?php
    onepress_site_logo();
    ?>
    </div>
    <div class=”headerMenuBar”>
    <ul class=”headerMenuUl”>
    <?php
    dynamic_sidebar(‘header-menu’);
    ?>

    </div>
    <div class=”header-right-wrapper”>

    <?php _e(‘Menu’, ‘onepress’); ?><span></span>
    <nav id=”site-navigation” class=”main-navigation” role=”navigation”>
    <ul class=”onepress-menu”>
    <?php wp_nav_menu(array(‘theme_location’ => ‘primary’, ‘container’ => ”, ‘items_wrap’ => ‘%3$s’)); ?>

    </nav>
    <!– #site-navigation –>
    </div>
    </div>
    </header><!– #masthead –>
    <?php
    }
    }

    I want to override that function into my child theme. I have made a php file with the same name in child theme

    add_action( ‘onepress_site_start’, ‘onepress_site_header’ );
    function onepress_site_header(){
    $header_width = get_theme_mod( ‘onepress_header_width’, ‘contained’ );
    $is_disable_sticky = sanitize_text_field( get_theme_mod( ‘onepress_sticky_header_disable’ ) );
    $classes = array(
    ‘site-header’, ‘header-‘.$header_width,
    );

    if ( $is_disable_sticky != 1 ) {
    $classes[] =’is-sticky no-scroll’;
    } else {
    $classes[] =’no-sticky no-scroll’;
    }

    $transparent = ‘no-t’;
    if ( onepress_is_transparent_header() ){
    $transparent = ‘is-t’;
    }
    $classes[] = $transparent;

    $pos = sanitize_text_field(get_theme_mod(‘onepress_header_position’, ‘top’));
    if ($pos == ‘below_hero’) {
    $classes[] = ‘h-below-hero’;
    } else {
    $classes[] = ‘h-on-top’;
    }

    ?>
    <header id=”masthead” class=”<?php echo esc_attr( join(‘ ‘, $classes ) ); ?>” role=”banner”>
    <div class=”container”>
    <div class=”site-branding”>
    <?php
    onepress_site_logo();
    ?>
    </div>

    <div class=”header-right-wrapper”>
    <?php _e(‘Menu’, ‘onepress’); ?><span></span>
    <div class=”headerMenuBar”>
    <ul class=”headerMenuUl”>
    <?php
    dynamic_sidebar(‘header-menu’);
    ?>

    </div>
    <nav id=”site-navigation” class=”main-navigation” role=”navigation”>
    <ul class=”onepress-menu”>
    <?php wp_nav_menu(array(‘theme_location’ => ‘primary’, ‘container’ => ”, ‘items_wrap’ => ‘%3$s’)); ?>

    </nav>
    <!– #site-navigation –>
    </div>
    </div>
    </header><!– #masthead –>
    <?php
    }

    difference is I changed position of dynamic_sidebar()

    I can’t override function. Nothing changes. Why?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Theme Author FameThemes

    (@famethemes)

    Hello @oehqoehfo,

    You should put these functions in main child theme’s functions.php. Don’t need to create the same file name in child theme.

    Let me know!

    Thread Starter oehqoehfo

    (@oehqoehfo)

    @famethemes thank you. Worked.

    Have one more question. Should I always add code to functions.php in child element if I want override any php files from parent theme?

    @oehqoehfo: No.You could only replace which function has function exists condition like this:

    if ( ! function_exists( ‘onepress_site_header’ ) ) { }

    Also, child themes are allowed to override templates, not simply arbitrary PHP files.

    In WordPress, a theme consists of a bunch of PHP files which are used as Templates. You can find a list of those files in the Template Hierarchy.

    Those specific template files can be overridden with new ones, but unless the parent theme has some special means for you to override other files, then files simply included by the parent as part of a support structure or as libraries for a pagebuilder piece of code cannot simply be overridden in that manner.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to override function in parent theme?’ is closed to new replies.