Fatal error: Class ‘WP_Customize_Control’ not found – WordPress

Im building my own wordperss theme and when starting on the WordPress Customizer for theme options I have come in a little bit of trouble.

Basically im trying to create a textarea and what I have read I need to create a extended class and then call it under the add_control function of WordPress.

Read More

I have tried this and all works well in customizer mode but as soon as I enter any other part of the site I get this error:

Fatal error: Class ‘WP_Customize_Control’ not found

As I say it works 100% within the customizer it’s self but any other page including admin I get this message.

Here is the class:

class ublxlportfolio_textarea extends WP_Customize_Control {
    public $type = 'textarea';

    public function render_content() {
        ?>
        <label>
        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
        <textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
        </label>
        <?php
    }
}

Do I need to wrap it in a conditional tag? If so what would that be??

Am I doing this all wrong?

Related posts

Leave a Reply

4 comments

  1. To clarify @Robert’s correct answer:

    Class WP_Customize_Control is loaded only when theme customizer is acutally used. So, you need to define your class within function bind to ‘customize_register’ action.

    Example:

    add_action( 'customize_register', 'my_customize_register' );
    
    function my_customize_register($wp_customize) {
    
      //class definition must be within my_customie_register function
      class ublxlportfolio_textarea extends WP_Customize_Control { ... }
    
      //other stuff
    }
    
  2. You need the following line before the class definition:

    include_once ABSPATH . 'wp-includes/class-wp-customize-control.php';
    

    I had the same issue and landed here from Google, hope this help someone!

  3. Reminder: In case if you just forgot to check if WP_Customize_Control class exists or not while extending it. This reminder might help you to debug this issue if you are in the page where theme customizer is not used; Since Class WP_Customize_Control is loaded only when theme customizer is actually used.

    if (class_exists('WP_Customize_Control')) {
        class yourCustomControlClass extends WP_Customize_Control {
           // control actions
        }
    }
    

    Cheers!