Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 298
Robert Avatar asked Nov 29 '22 02:11

Robert


1 Answers

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
}
like image 154
fandasson Avatar answered Dec 05 '22 22:12

fandasson