1

I've developed many themes, however, I'm new to building child-themes and "especially" trying to implement classes in child themes. So I've found...

Is it possible to load a WP Customizer class in a child theme? In my case, here is what I'm doing in my child theme:

functions.php

// Setup the Child Theme URL
define('child_template_directory', dirname( get_bloginfo('stylesheet_url')) );
// Get the files in the inc folder
$files_to_require = array( 'theme-customizer', );
foreach( $files_to_require as $file ) {
 locate_template ( "inc/{$file}.php", true, true );
}

inc/theme-customizer.php

if ( ! function_exists( 'my_customizer_frontend' ) && ! class_exists( 'My_Customizer_Frontend' ) ) {
 function my_customizer_frontend() {
 load_template( child_template_directory . '/inc/customizer/class-my-customize-frontend.php' );
 new My_Customizer_Frontend(); //This is line 34
 }
 add_action( 'init', 'my_customizer_frontend' );
}

inc/customizer/class-my-customizer-frontend.php

class My_Customizer_Frontend {
/* Do some stuff */
}

Using the example code above, I cannot get past the following error:

Fatal error: Class 'My_Customizer_Frontend' not found in /var/www/html/mydomain.com/wp-content/themes/child-theme/inc/theme-customizer.php on line 34

I've been trying to overcome this for a full day and am just spinning my wheels now.

I've gone through all the codex but this seems to be out of scope of anything written anywhere.

Any advice would be greatly appreciated.

Best Regards,

2

I think that the error message is clear enough: the class was not found. The first thing you should do is to verify the path you are using to load the file where the class is defined.

If you do that and check the value of child_template_directory constant, you will see that it has not trailing slash, so this line:

load_template( child_template_directory . 'inc/customizer/class-my-customize-frontend.php' );

Should be:

load_template( child_template_directory . '/inc/customizer/class-my-customize-frontend.php' );

Anyway, there is a better way to get the child theme directoy path: get_stylesheet_directory() function.

I would do:

load_template( get_stylesheet_directory() . '/inc/customizer/class-my-customize-frontend.php' );

Or, at least I would define the constant in this way:

define('child_template_directory', get_stylesheet_directory() );

PD: Your porlbem It is not a WP Customizer Fatal Error.

  • 1
    Thanks @cybmeta You got it right on with the get_stylesheet_directory() call, I originally had the bloginfo... because I had wanted to create some predefined paths. My laziness had sent me in that direction. After updating that, it works so on to resolving my next error. BTW: I had the "/" prior to inc/ in every case, but entered it wrong above. – Dennis Dec 4 '15 at 17:34

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.