• Salih K

    (@salihkulangara)


    I am using a Purchased theme, in that they have used OOP approach, looks good. But the issue I have is to get a desired result for my design I have to add some extra feature. So I am trying to extend a particular class in the Parent theme from my child theme. But I could not do that in my functions.php, getting an error says Parent Class not found. My class code is something like this

    class MyClasss extends ParentTheme_Templates {
    public static function header() {
        $type = ParentTheme_Global::instance()->get_header_type();
    
        get_template_part( 'mycomponents/headers/header', $type );
    }

    but I can use the same code for my class in my template file(I tried in header.php file) and it works, but I know this is not a proper way. So is there any proper method to do this. I can see in the codex:

    Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)

    It seems It might be because functions.php it is loaded right before the parent’s file.
    So Is there any solution for this, or any workaround?

    • This topic was modified 6 years, 3 months ago by Salih K.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Child themes are loaded before the parent, so you will need to define the class inside a hook that runs after the parent theme has loaded, so that the parent theme class exists to be extended. after_setup_theme would be the logical choice.

    One approach would be to define your extended class inside its own file, which I’ll call includes/class-my-class.php for this example, and then include that file within the hook, like this:

    function mytheme_includes() {
    	require_once get_theme_file_path( 'includes/class-my-class.php' );
    }
    add_action( 'after_setup_theme', 'mytheme_includes' );
    
    • This reply was modified 6 years, 3 months ago by Jacob Peattie.
    Thread Starter Salih K

    (@salihkulangara)

    Thanks brother

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to extend a parent theme class from child theme?’ is closed to new replies.