1

I am trying to create a plugin that lets me extend upon a class defined in a particular theme.

The theme has a class of 'ET_Builder_Module'

Within the theme there are various other classes defined that extend this, for example:

class ET_Builder_Module_Blurb extends ET_Builder_Module

I would like to add my own classes to extend the theme however I get the following error:

Fatal error: Class 'ET_Builder_Module' not found

I have used the following code:

class NOOU_ET_Builder_Test_Module extends ET_Builder_Module

I simply copy and pasted one of the classes from the theme and renamed the class

Would any one be able to show me how to extend a class from a theme within a plugin?

  • what action are trying to extend the class on? plugins load before themes, so you need to hook your code to run later than the theme code runs. – Milo Sep 26 '15 at 0:55
1

Make your class visible to PHP after the theme has declared the parent class.

Plugins are loaded first, the theme maybe later. This can be overridden.

Theme code is available when the action after_setup_theme is fired, so you could use it like this:

add_action( 'after_setup_theme', function() {
    require 'ChildClass.php';   
});

However, if the theme loads the parent class later, you might run into the same problem: You don't know when it is really available.

The alternative is an autloader: Do not load classes in advance. Let PHP wait instead until you create a new instance of a class, then let a registered autoloader include the proper file.

Here is an overly simple example that you can put into your plugin's main file. Make sure you file name matches the class name, eg. ET_Builder_Module_Blurb.php.

spl_autoload_register( function( $name ) {

    $path = __DIR__ . "/$name.php";

    if ( file_exists( $path ) )
        require $path;
});
  • Ahh I didn't realise that plugins were loaded after the themes. The function in the theme is for a page builder which only runs when editing a page so I think the autoloader is the right method here. I will try this and see what I get out of it. Should I define name as the file name of MY class extension? – Ben H Sep 26 '15 at 7:13
  • @BenH See my update, I tried to make that more clear. – fuxia Sep 26 '15 at 7:19
  • Thank you that made it a little clearer for me. It doesn't seem to be working as my addition to the page builder doesnt show up. however if i paste my addition directly into the theme it does so my class for that is working correctly. is there a way I can add an admin notice when it loads the class? I am not getting any errors which is good. – Ben H Sep 26 '15 at 7:29
  • You can write something temporarily in your constructor: function __construct() { add_action('admin_notices', function(){print '<div class="updated">' . __CLASS__ . ' loaded!</div>';});} – fuxia Sep 26 '15 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.