I'm once again sort of stuck with my Customizer API. I've worked quiet a lot on the Theme Customization of my Theme and my functions.php is getting a bit out of hand. Is there a way I can put all these "panels/sections/settings/controls" in an extra file and just load that file into the functions.php?

And even thoe is there a way I can load that file only if the Customizer is open?

Thanks in advance!


Yes. You can load the code in the customize_register action. One example:

// File: functions.php
add_action( 'customize_register', function( $wp_customize ) {
    require_once dirname( __FILE__ ) . '/inc/customize.php';
    wpse256532_customize_register( $wp_customize );
} );

And the inc/customize.php file:

// File: customize.php
function wpse256532_customize_register( $wp_customize ) {
    $wp_customize->add_setting( /* ... */ );
    $wp_customize->add_control( /* ... */ );
    // ...
// ... any additional customizer classes and other includes ...
  • This somehow doesn't work for me ... The website loads but if I go into the customizer, it looks like an php error. If I change the 'customize_register' into 'some_customize_register' the customizer loads but doesn't have the custom panels/sections.
    – Pelle2010
    Feb 17 '17 at 7:59
  • 1
    You're right. I neglected to pass along the $wp_customize argument from the customize_register action. I just updated the example to include this. Feb 18 '17 at 0:31
  • This once again didn't work for me. Am I doing something wrong? I change the 'wpse256532_customize_register' to 'mytheme_customize_register' in both files and change the '/inc/customize.php' into '/inc/mytheme-customize.php'. Its the same thing ... I get a white site (php error) and if I change the 'customize_register' into 'mytheme_customize_register' it loads the customizer but not my custom Pannels/Settings.
    – Pelle2010
    Feb 20 '17 at 7:48
  • Changing customize_register to mytheme_customize_register will just prevent the logic from being called at all, since mytheme_customize_register is not a valid action. There's a chance that you're on an older version of PHP that doesn't recognize __DIR__ so I changed the example to use dirname( __FILE__ ) instead. Feb 20 '17 at 19:11
  • I've now used this method on a new theme and it works like a charm ... no idea what was wrong with the earlier theme I created back than ... thank you!
    – Pelle2010
    Jun 16 '17 at 10:30

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.