Fatal error: Class 'WP_Customize_Control' not found - WordPress
Asked Answered
S

4

6

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?

Schaller answered 13/5, 2013 at 22:47 Comment(0)
S
2

Found out that the class needs to come within the register function!

Schaller answered 13/5, 2013 at 22:53 Comment(0)
R
22

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
}
Reest answered 29/7, 2014 at 9:40 Comment(2)
Just make sure that you add your custom control PRIOR to attempting to utilize it. I made that mistake and had to facepalm once I figured out my stupidity.Mommy
Indeed. tis is the proper way to implement.Huddle
C
6

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!

Cleavers answered 21/1, 2018 at 15:57 Comment(0)
S
2

Found out that the class needs to come within the register function!

Schaller answered 13/5, 2013 at 22:53 Comment(0)
K
0

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!

Kimi answered 24/7, 2020 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.