Skip to content

Update Handle to work with OOP and allow it to extend its parent theme #23

@adampatterson

Description

@adampatterson

WordPress parent theme usually defines an action that calls a function. You wrap that function in function_exists() so that you can overwrite it in a child theme.

add_filter('the_title', 'markdown_title');
if ( ! function_exists('markdown_title')) {
    function markdown_title($post_title) {}
}

The Child theme simply sets a new function markdown_title()

Alternatively, you use OOP in the parent theme and then namespace everything nicely but are now you are using composer autoload in your parent theme and as well as your child theme creating another namespace for that theme and extending parent classes to adjust parent method functionality.

I have not tried this yet, and not many “developer” themes cover this. But because it is namespaced and the scope of the actions and filters are typical $this or the namespace of another class. I would have to use __construct to register the actions which then presumably extend, call parent::__construct(); and hopefully the WordPress actions do their job and use the new method from the child theme.

namespace ParentTheme;
class myClass
{

    public function __construct()
    {
        add_filter('the_title', [$this, 'markdown_title']);
    }

    public function markdown_title($post_title)
    {
        // Make Markdown
    }
}

namespace ChildTheme;
class anotherClass extends myClass {
    public function __construct()
    {
        parent::__construct();
    }
    
    public function markdown_title($post_title)
    {
        // Make Awesome Markdown
    }
}

Actually, now that I look at this, I have no idea if it would work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions